diff options
author | crupest <crupest@outlook.com> | 2021-04-28 19:20:40 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-28 19:20:40 +0800 |
commit | d922b3241245c9bc1ca6cff8ac69dd7659a958f1 (patch) | |
tree | ecad89ac4933980a54b643d899c339c7d86ae900 /BackEnd/Timeline/Services/Mapper/MapperExtensions.cs | |
parent | 66835dcd6812d40bca8d271a7773637f74aaa8d4 (diff) | |
download | timeline-d922b3241245c9bc1ca6cff8ac69dd7659a958f1.tar.gz timeline-d922b3241245c9bc1ca6cff8ac69dd7659a958f1.tar.bz2 timeline-d922b3241245c9bc1ca6cff8ac69dd7659a958f1.zip |
refator: ...
Diffstat (limited to 'BackEnd/Timeline/Services/Mapper/MapperExtensions.cs')
-rw-r--r-- | BackEnd/Timeline/Services/Mapper/MapperExtensions.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/Mapper/MapperExtensions.cs b/BackEnd/Timeline/Services/Mapper/MapperExtensions.cs new file mode 100644 index 00000000..03dd1189 --- /dev/null +++ b/BackEnd/Timeline/Services/Mapper/MapperExtensions.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Mvc;
+using System.Collections.Generic;
+using System.Security.Claims;
+using System.Threading.Tasks;
+
+namespace Timeline.Services.Mapper
+{
+ public static class MapperExtensions
+ {
+ public static async Task<List<TDestination>> MapListAsync<TSource, TDestination>(this IMapper<TSource, TDestination> mapper, IEnumerable<TSource> source, IUrlHelper urlHelper, ClaimsPrincipal? user)
+ {
+ var result = new List<TDestination>();
+ foreach (var s in source)
+ {
+ result.Add(await mapper.MapAsync(s, urlHelper, user));
+ }
+ return result;
+ }
+
+ public static Task<TDestination> MapAsync<TDestination>(this IGenericMapper mapper, object source, IUrlHelper urlHelper, ClaimsPrincipal? user)
+ {
+ var method = typeof(IGenericMapper).GetMethod(nameof(IGenericMapper.MapAsync));
+ var m = method!.MakeGenericMethod(source.GetType(), typeof(TDestination))!;
+ return (Task<TDestination>)m.Invoke(mapper, new object?[] { source, urlHelper, user })!;
+ }
+
+ public static async Task<List<TDestination>> MapListAsync<TSource, TDestination>(this IGenericMapper mapper, IEnumerable<TSource> source, IUrlHelper urlHelper, ClaimsPrincipal? user)
+ {
+ var result = new List<TDestination>();
+ foreach (var s in source)
+ {
+ result.Add(await mapper.MapAsync<TSource, TDestination>(s, urlHelper, user));
+ }
+ return result;
+ }
+
+ public static async Task<List<TDestination>> MapListAsync<TDestination>(this IGenericMapper mapper, IEnumerable<object> source, IUrlHelper urlHelper, ClaimsPrincipal? user)
+ {
+ var result = new List<TDestination>();
+ foreach (var s in source)
+ {
+ result.Add(await mapper.MapAsync<TDestination>(s, urlHelper, user));
+ }
+ return result;
+ }
+ }
+}
|