Example #1
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 UniTask <TDestinationCollection> MapCollectionAsync <TSourceCollection, TSource, TDestinationCollection, TDestination>(
            this IAsyncImmutableMapper <TSource, TDestination> mapper,
            TSourceCollection source,
            TDestinationCollection destination,
            CancellationToken cancellationToken = default)
            where TSourceCollection : IEnumerable <TSource>
            where TDestinationCollection : ICollection <TDestination>
            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 sourceCount = source.Count();
            var tasks       = new UniTask <TDestination> [sourceCount];
            var i           = 0;

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

            await UniTask.WhenAll(tasks);

            foreach (var task in tasks)
            {
#pragma warning disable VSTHRD103 // Call async methods when in an async method.
                destination.Add(task.GetAwaiter().GetResult());
#pragma warning restore VSTHRD103 // Call async methods when in an async method.
            }

            return(destination);
        }
Example #2
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 IAsyncImmutableMapper <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 = await mapper.MapAsync(source, cancellationToken);

            return(destination);
        }
Example #3
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 IAsyncImmutableMapper <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 <TDestination> [sourceCount];
            var destination = new List <TDestination>(sourceCount);
            var i           = 0;

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

            await UniTask.WhenAll(tasks);

            for (var j = 0; j < tasks.Length; ++j)
            {
#pragma warning disable VSTHRD103 // Call async methods when in an async method.
                destination.Insert(j, tasks[j].GetAwaiter().GetResult());
#pragma warning restore VSTHRD103 // Call async methods when in an async method.
            }

            return(destination);
        }
        /// <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 IAsyncImmutableMapper <TSource, TDestination> mapper,
            IAsyncEnumerable <TSource> source,
            [EnumeratorCancellation] CancellationToken cancellationToken = default)
            where TDestination : new()
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

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

            await foreach (var sourceItem in source.ConfigureAwait(false).WithCancellation(cancellationToken))
            {
                var destinationItem = await mapper.MapAsync(sourceItem, cancellationToken).ConfigureAwait(false);

                yield return(destinationItem);
            }
        }
Example #5
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="mapper">The mapper.</param>
        /// <param name="source">The source objects.</param>
        /// <param name="cancellationToken">The cancellation token.</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 UniTask <ObservableCollection <TDestination> > MapObservableCollectionAsync <TSource, TDestination>(
            this IAsyncImmutableMapper <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 <TDestination> [sourceCount];
            var destination = new ObservableCollection <TDestination>();

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

            await UniTask.WhenAll(tasks);

            for (var i = 0; i < tasks.Length; ++i)
            {
#pragma warning disable VSTHRD103 // Call async methods when in an async method.
                destination.Insert(i, tasks[i].GetAwaiter().GetResult());
#pragma warning restore VSTHRD103 // Call async methods when in an async method.
            }

            return(destination);
        }
 /// <summary>
 /// Maps the enumerable of <typeparamref name="TSource"/> into an immutable 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>An immutable 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 <ImmutableList <TDestination> > MapImmutableListAsync <TSource, TDestination>(
     this IAsyncImmutableMapper <TSource, TDestination> mapper,
     IEnumerable <TSource> source,
     CancellationToken cancellationToken = default)
     where TDestination : new() =>
 ImmutableList.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));