/// <summary> /// Translates the collection of <typeparamref name="TSource" /> into a collection of type /// <typeparamref name="TCollection" /> containing objects of type <typeparamref name="TDestination" />. /// </summary> /// <typeparam name="TCollection">The type of the 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 objects.</param> /// <returns>A collection of type <typeparamref name="TCollection"/> containing objects 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 <TCollection> TranslateCollection <TCollection, TSource, TDestination>( this IAsyncTranslator <TSource, TDestination> translator, IEnumerable <TSource> source) where TCollection : ICollection <TDestination>, new() where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var collection = new TCollection(); var tasks = new List <Task>(source.Count()); foreach (var item in source) { var destination = new TDestination(); collection.Add(destination); var task = translator.Translate(item, destination); tasks.Add(task); } await Task.WhenAll(tasks); return(collection); }
/// <summary> /// Translates 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="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[]> TranslateArray <TSource, TDestination>( this IAsyncTranslator <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 array = new TDestination[source.Count()]; var tasks = new Task[source.Count()]; var i = 0; foreach (var item in source) { var destination = new TDestination(); array[i] = destination; var task = translator.Translate(item, destination); tasks[i] = task; ++i; } await Task.WhenAll(tasks); return(array); }
/// <summary> /// Translates 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="sourceCollection">The source collection.</param> /// <param name="destinationCollection">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="sourceCollection"/> is /// <c>null</c>.</exception> public static async Task <TDestination[]> TranslateArray <TSourceCollection, TSource, TDestination>( this IAsyncTranslator <TSource, TDestination> translator, TSourceCollection sourceCollection, TDestination[] destinationCollection, int?sourceCount = null) where TSourceCollection : IEnumerable <TSource> where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (sourceCollection == null) { throw new ArgumentNullException(nameof(sourceCollection)); } var tasks = new Task[sourceCount ?? sourceCollection.Count()]; var i = 0; foreach (var item in sourceCollection) { var destination = new TDestination(); destinationCollection[i] = destination; tasks[i] = translator.Translate(item, destination); ++i; } await Task.WhenAll(tasks); return(destinationCollection); }
/// <summary> /// Translates 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="translator">The translator.</param> /// <param name="source">The source object.</param> /// <returns>The translated 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> Translate <TSource, TDestination>( this IAsyncTranslator <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 = new TDestination(); await translator.Translate(source, destination); return(destination); }