Esempio n. 1
0
        /// <summary>
        /// Maps the array of <typeparamref name="TSource"/> into a list of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>A list of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <List <TDestination> > MapListAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSource[] source)
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Length;
            var tasks       = new Task[sourceCount];
            var destination = new List <TDestination>(sourceCount);

            for (var i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = mapper.MapAsync(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(destination);
        }
Esempio n. 2
0
        /// <summary>
        /// Maps the <see cref="IAsyncEnumerable{TSource}"/> into <see cref="IAsyncEnumerable{TDestination}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source asynchronous enumerable.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>An <see cref="IAsyncEnumerable{TDestination}"/> collection.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async IAsyncEnumerable <TDestination> MapEnumerableAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            IAsyncEnumerable <TSource> source,
            [EnumeratorCancellation] CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            await foreach (var sourceItem in source.ConfigureAwait(false).WithCancellation(cancellationToken))
            {
                var destinationItem = Factory <TDestination> .CreateInstance();

                await mapper.MapAsync(sourceItem, destinationItem).ConfigureAwait(false);

                yield return(destinationItem);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Maps the array of <typeparamref name="TSource"/> into an observable collection of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>An observable collection of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <ObservableCollection <TDestination> > MapObservableCollection <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            TSource[] source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Length;
            var tasks       = new Task[sourceCount];
            var destination = new ObservableCollection <TDestination>();

            for (int i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = translator.Map(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
Esempio n. 4
0
        /// <summary>
        /// Maps the array of <typeparamref name="TSource"/> into a collection of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A collection of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async UniTask <Collection <TDestination> > MapCollectionAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSource[] source,
            CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Length;
            var tasks       = new UniTask[sourceCount];
            var destination = new Collection <TDestination>();

            for (var i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = new TDestination();
                destination.Insert(i, destinationItem);
                tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken);
            }

            await UniTask.WhenAll(tasks);

            return(destination);
        }
Esempio n. 5
0
        /// <summary>
        /// Maps the enumerable of <typeparamref name="TSource"/> into an observable collection of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>An observable collection of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <ObservableCollection <TDestination> > MapObservableCollectionAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            IEnumerable <TSource> source)
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count();
            var tasks       = new Task[sourceCount];
            var destination = new ObservableCollection <TDestination>();
            var i           = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = mapper.MapAsync(sourceItem, destinationItem);
                ++i;
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(destination);
        }
        /// <summary>
        /// Maps the collection of <typeparamref name="TSource"/> into a collection of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>A collection of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <Collection <TDestination> > MapCollectionAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            Collection <TSource> source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count;
            var tasks       = new Task[sourceCount];
            var destination = new Collection <TDestination>();

            for (var i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = translator.MapAsync(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(destination);
        }
Esempio n. 7
0
        /// <summary>
        /// Maps the list of <typeparamref name="TSource"/> into an array of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>An array of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <TDestination[]> MapArray <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            List <TSource> source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count;
            var tasks       = new Task[sourceCount];
            var destination = new TDestination[sourceCount];

            for (int i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination[i] = destinationItem;
                tasks[i]       = translator.Map(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
Esempio n. 8
0
        /// <summary>
        /// Maps the list of <typeparamref name="TSource"/> into an array of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>An array of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async UniTask <TDestination[]> MapArrayAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            List <TSource> source,
            CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count;
            var tasks       = new UniTask[sourceCount];
            var destination = new TDestination[sourceCount];

            for (var i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = new TDestination();
                destination[i] = destinationItem;
                tasks[i]       = mapper.MapAsync(sourceItem, destinationItem, cancellationToken);
            }

            await UniTask.WhenAll(tasks);

            return(destination);
        }
Esempio n. 9
0
        /// <summary>
        /// Maps the enumerable of <typeparamref name="TSource"/> into a list of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A list of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async UniTask <List <TDestination> > MapListAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            IEnumerable <TSource> source,
            CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count();
            var tasks       = new UniTask[sourceCount];
            var destination = new List <TDestination>(sourceCount);
            var i           = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = new TDestination();
                destination.Insert(i, destinationItem);
                tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken);
                ++i;
            }

            await UniTask.WhenAll(tasks);

            return(destination);
        }
        /// <summary>
        /// Maps an enumerable of <typeparamref name="TSource"/> into observable collection of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">Source objects type</typeparam>
        /// <typeparam name="TDestination">Destination objects type</typeparam>
        /// <param name="translator">Translator.</param>
        /// <param name="source">Source objects.</param>
        /// <returns>Observable collection of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <ObservableCollection <TDestination> > MapObservableCollection <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            IEnumerable <TSource> source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            int sourceCount = source.Count();

            Task[] tasks = new Task[sourceCount];
            ObservableCollection <TDestination> destination = new ObservableCollection <TDestination>();
            int i = 0;

            foreach (TSource sourceItem in source)
            {
                TDestination destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = translator.Map(sourceItem, destinationItem);
                ++i;
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
        /// <summary>
        /// Maps a collection of <typeparamref name="TSource"/> into list of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">Source objects type</typeparam>
        /// <typeparam name="TDestination">Destination objects type</typeparam>
        /// <param name="translator">Translator.</param>
        /// <param name="source">Source objects.</param>
        /// <returns>List of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <List <TDestination> > MapList <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            Collection <TSource> source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            int sourceCount = source.Count;

            Task[] tasks = new Task[sourceCount];
            List <TDestination> destination = new List <TDestination>(sourceCount);

            for (int i = 0; i < sourceCount; ++i)
            {
                TSource      sourceItem      = source[i];
                TDestination destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = translator.Map(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
Esempio n. 12
0
        /// <summary>
        /// Maps the collection of <typeparamref name="TSource"/> into an array of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>An array of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <TDestination[]> MapArrayAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            Collection <TSource> source)
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count;
            var tasks       = new Task[sourceCount];
            var destination = new TDestination[sourceCount];

            for (var i = 0; i < sourceCount; ++i)
            {
                var sourceItem      = source[i];
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination[i] = destinationItem;
                tasks[i]       = mapper.MapAsync(sourceItem, destinationItem);
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(destination);
        }
Esempio n. 13
0
        /// <summary>
        /// Maps the enumerable of <typeparamref name="TSource"/> into a list of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source objects.</param>
        /// <returns>A list of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <List <TDestination> > MapList <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            IEnumerable <TSource> source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sourceCount = source.Count();
            var tasks       = new Task[sourceCount];
            var destination = new List <TDestination>(sourceCount);
            var i           = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Insert(i, destinationItem);
                tasks[i] = translator.Map(sourceItem, destinationItem);
                ++i;
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
Esempio n. 14
0
        /// <summary>Add new async mapper to configurations</summary>
        /// <exception cref="ArgumentNullException">
        /// When mapper is null
        /// </exception>
        /// <typeparam name="TSource">Type of source</typeparam>
        /// <typeparam name="TTarget">Type of target</typeparam>
        /// <param name="mapper">Mapper which maps source to target type</param>
        public void AddAsyncMapper <TSource, TTarget>(IAsyncMapper <TSource, TTarget> mapper)
            where TSource : class
            where TTarget : class
        {
            if (mapper == null)
            {
                throw new ArgumentNullException("mapper");
            }

            AddMapper(typeof(TSource), typeof(TTarget), mapper);
        }
Esempio n. 15
0
 protected CoreDataSource(
     IAppLogger logger,
     IAsyncMapper <string, TVersionedModel, TKey> toVersionedMapper,
     IAsyncMapper <TVersionedModel, TCoreData, TKey> toCoreMapper,
     ProgramOptions programOptions,
     DataDirectoryPath dataPath,
     IWebRequestClient webClient)
 {
     _logger            = logger;
     _toVersionedMapper = toVersionedMapper;
     _toCoreMapper      = toCoreMapper;
     _programOptions    = programOptions;
     DataPath           = dataPath;
     _webClient         = webClient;
 }
Esempio n. 16
0
        /// <summary>
        /// Maps the collection of <typeparamref name="TSource" /> into a collection of type
        /// <typeparamref name="TDestinationCollection" /> containing objects of type <typeparamref name="TDestination" />.
        /// </summary>
        /// <typeparam name="TSourceCollection">The type of the source collection.</typeparam>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestinationCollection">The type of the destination collection.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source collection.</param>
        /// <param name="destination">The destination collection.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A collection of type <typeparamref name="TDestinationCollection"/> containing objects of type
        /// <typeparamref name="TDestination" />.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper" /> or <paramref name="source" /> is
        /// <c>null</c>.</exception>
        public static async Task <TDestinationCollection> MapCollectionAsync <TSourceCollection, TSource, TDestinationCollection, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSourceCollection source,
            TDestinationCollection destination,
            CancellationToken cancellationToken = default)
            where TSourceCollection : IEnumerable <TSource>
            where TDestinationCollection : ICollection <TDestination>
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination is null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var sourceCount = source.Count();
            var tasks       = new Task[sourceCount];
            var i           = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination.Add(destinationItem);
                tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken);
                ++i;
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(destination);
        }
Esempio n. 17
0
        /// <summary>
        /// Maps the specified source object to a new object with a type of <typeparamref name="TDestination"/>.
        /// </summary>dotnet test
        /// <typeparam name="TSource">The type of the source object.</typeparam>
        /// <typeparam name="TDestination">The type of the destination object.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source object.</param>
        /// <returns>The mapped object of type <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator" /> or <paramref name="source" /> is
        /// <c>null</c>.</exception>
        public static async Task <TDestination> Map <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            TSource source)
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var destination = Factory <TDestination> .CreateInstance();

            await translator.Map(source, destination);

            return(destination);
        }
Esempio n. 18
0
        /// <summary>
        /// Maps the specified source object to a new object with a type of <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source object.</typeparam>
        /// <typeparam name="TDestination">The type of the destination object.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source object.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The mapped object of type <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper" /> or <paramref name="source" /> is
        /// <c>null</c>.</exception>
        public static async UniTask <TDestination> MapAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSource source,
            CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var destination = new TDestination();
            await mapper.MapAsync(source, destination, cancellationToken);

            return(destination);
        }
Esempio n. 19
0
        /// <summary>
        /// Maps the specified source object to a new object with a type of <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source object.</typeparam>
        /// <typeparam name="TDestination">The type of the destination object.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source object.</param>
        /// <returns>The mapped object of type <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper" /> or <paramref name="source" /> is
        /// <c>null</c>.</exception>
        public static async Task <TDestination> MapAsync <TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSource source)
            where TDestination : new()
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var destination = Factory <TDestination> .CreateInstance();

            await mapper.MapAsync(source, destination).ConfigureAwait(false);

            return(destination);
        }
Esempio n. 20
0
        /// <summary>
        /// Maps the collection of <typeparamref name="TSource"/> into an array of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSourceCollection">The type of the source collection.</typeparam>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="source">The source collection.</param>
        /// <param name="destination">The destination collection.</param>
        /// <param name="sourceCount">The number of items in the source collection.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>An array of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async UniTask <TDestination[]> MapArrayAsync <TSourceCollection, TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> mapper,
            TSourceCollection source,
            TDestination[] destination,
            int?sourceCount = null,
            CancellationToken cancellationToken = default)
            where TSourceCollection : IEnumerable <TSource>
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var tasks = new UniTask[sourceCount ?? source.Count()];
            var i     = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = new TDestination();
                destination[i] = destinationItem;
                tasks[i]       = mapper.MapAsync(sourceItem, destinationItem, cancellationToken);

                ++i;
            }

            await UniTask.WhenAll(tasks);

            return(destination);
        }
Esempio n. 21
0
        /// <summary>
        /// Maps the collection of <typeparamref name="TSource"/> into an array of
        /// <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TSourceCollection">The type of the source collection.</typeparam>
        /// <typeparam name="TSource">The type of the source objects.</typeparam>
        /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
        /// <param name="translator">The translator.</param>
        /// <param name="source">The source collection.</param>
        /// <param name="destination">The destination collection.</param>
        /// <param name="sourceCount">The number of items in the source collection.</param>
        /// <returns>An array of <typeparamref name="TDestination"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is
        /// <c>null</c>.</exception>
        public static async Task <TDestination[]> MapArray <TSourceCollection, TSource, TDestination>(
            this IAsyncMapper <TSource, TDestination> translator,
            TSourceCollection source,
            TDestination[] destination,
            int?sourceCount = null)
            where TSourceCollection : IEnumerable <TSource>
            where TDestination : new()
        {
            if (translator == null)
            {
                throw new ArgumentNullException(nameof(translator));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var tasks = new Task[sourceCount ?? source.Count()];
            var i     = 0;

            foreach (var sourceItem in source)
            {
                var destinationItem = Factory <TDestination> .CreateInstance();

                destination[i] = destinationItem;
                tasks[i]       = translator.Map(sourceItem, destinationItem);

                ++i;
            }

            await Task.WhenAll(tasks);

            return(destination);
        }
Esempio n. 22
0
 public Worker(IAsyncMapper <SourceClass> mapper)
 {
     this.mapper = mapper;
 }
 /// <summary>
 /// Maps the enumerable of <typeparamref name="TSource"/> into an immutable array of
 /// <typeparamref name="TDestination"/>.
 /// </summary>
 /// <typeparam name="TSource">The type of the source objects.</typeparam>
 /// <typeparam name="TDestination">The type of the destination objects.</typeparam>
 /// <param name="mapper">The mapper.</param>
 /// <param name="source">The source objects.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>An immutable array of <typeparamref name="TDestination"/>.</returns>
 /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
 /// <c>null</c>.</exception>
 public static async Task <ImmutableArray <TDestination> > MapImmutableArrayAsync <TSource, TDestination>(
     this IAsyncMapper <TSource, TDestination> mapper,
     IEnumerable <TSource> source,
     CancellationToken cancellationToken = default)
     where TDestination : new() =>
 ImmutableArray.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));