forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeAdapterExtensions.cs
105 lines (92 loc) · 3.7 KB
/
TypeAdapterExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Mapster
{
public static class TypeAdapterExtensions
{
const string ASYNC_KEY = "Mapster.Async.tasks";
internal static U GetValueOrDefault<T, U>(this IDictionary<T, U> dict, T key)
{
return dict.TryGetValue(key, out var value) ? value : default;
}
/// <summary>
/// Setup async operation
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="setter"></param>
/// <param name="action"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static TypeAdapterSetter<TDestination> AfterMappingAsync<TDestination>(
this TypeAdapterSetter<TDestination> setter, Func<TDestination, Task> action)
{
setter.AfterMapping(dest =>
{
var tasks = (List<Task>) MapContext.Current?.Parameters.GetValueOrDefault(ASYNC_KEY);
if (tasks == null)
throw new InvalidOperationException("Mapping contains async function, please use BuildAdapter.AdaptToTypeAsync instead");
var task = action(dest);
tasks.Add(task);
});
return setter;
}
/// <summary>
/// Setup async operation
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TDestination"></typeparam>
/// <param name="setter"></param>
/// <param name="action"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static TypeAdapterSetter<TSource, TDestination> AfterMappingAsync<TSource, TDestination>(
this TypeAdapterSetter<TSource, TDestination> setter, Func<TSource, TDestination, Task> action)
{
setter.AfterMapping((src, dest) =>
{
var tasks = (List<Task>) MapContext.Current?.Parameters.GetValueOrDefault(ASYNC_KEY);
if (tasks == null)
throw new InvalidOperationException("Mapping contains async function, please use BuildAdapter.AdaptToTypeAsync instead");
var task = action(src, dest);
tasks.Add(task);
});
return setter;
}
/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptToTypeAsync<TDestination>(this IAdapterBuilder builder)
{
var tasks = new List<Task>();
builder.Parameters[ASYNC_KEY] = tasks;
using (MapContextScope.RequiresNew())
{
var result = builder.AdaptToType<TDestination>();
await Task.WhenAll(tasks);
return result;
}
}
/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <param name="destination">Destination object to map.</param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptToAsync<TDestination>(this IAdapterBuilder builder, TDestination destination)
{
var tasks = new List<Task>();
builder.Parameters[ASYNC_KEY] = tasks;
using (MapContextScope.RequiresNew())
{
var result = builder.AdaptTo(destination);
await Task.WhenAll(tasks);
return result;
}
}
}
}