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 | e16c958e5ba47834dc1624e09ed8e5074a60d1c6 (patch) | |
tree | af8de5669e0ade7d1425d7c5599d438a6cf1c38c /BackEnd/Timeline/Services/Mapper/MapperExtensions.cs | |
parent | 29453339a7ba744dc19f730ffbd71d6bff01f25b (diff) | |
download | timeline-e16c958e5ba47834dc1624e09ed8e5074a60d1c6.tar.gz timeline-e16c958e5ba47834dc1624e09ed8e5074a60d1c6.tar.bz2 timeline-e16c958e5ba47834dc1624e09ed8e5074a60d1c6.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;
+ }
+ }
+}
|