-
Notifications
You must be signed in to change notification settings - Fork 49
/
GatewayProcessor.cs
169 lines (140 loc) · 5.53 KB
/
GatewayProcessor.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using AutoMapper;
using BuildingBlocks.Abstractions.CQRS.Command;
using BuildingBlocks.Abstractions.CQRS.Query;
using BuildingBlocks.Abstractions.Messaging;
using BuildingBlocks.Abstractions.Web;
using BuildingBlocks.Abstractions.Web.Module;
using BuildingBlocks.Web.Module;
using MediatR;
namespace BuildingBlocks.Web;
public class GatewayProcessor<TModule> : IGatewayProcessor<TModule>
where TModule : class, IModuleDefinition
{
private readonly IServiceProvider _serviceProvider;
public GatewayProcessor(IServiceProvider serviceProvider)
{
// https://blog.stephencleary.com/2016/12/eliding-async-await.html
var compositionRoot = CompositionRootRegistry.GetByModule<TModule>();
_serviceProvider = compositionRoot?.ServiceProvider ?? serviceProvider;
}
public async ValueTask ExecuteScopeAsync(Func<IServiceProvider, ValueTask> action)
{
await using var scope = _serviceProvider.CreateAsyncScope();
await action(scope.ServiceProvider);
}
public async ValueTask<T> ExecuteScopeAsync<T>(Func<IServiceProvider, ValueTask<T>> action)
{
await using var scope = _serviceProvider.CreateAsyncScope();
var result = await action(scope.ServiceProvider);
return result;
}
public async Task<TResponse> SendCommandAsync<TResponse>(
ICommand<TResponse> request,
CancellationToken cancellationToken = default)
where TResponse : notnull
{
return await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
return await commandProcessor.SendAsync(request, cancellationToken);
});
}
public async Task SendCommandAsync<T>(T request, CancellationToken cancellationToken = default)
where T : class, ICommand
{
await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
return await commandProcessor.SendAsync(request, cancellationToken);
});
}
public async Task ExecuteCommand<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : ICommand
{
await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
await commandProcessor.SendAsync(command, cancellationToken);
});
}
public async Task<TResult> ExecuteCommand<TCommand, TResult>(
TCommand command,
CancellationToken cancellationToken = default)
where TCommand : ICommand<TResult>
where TResult : notnull
{
return await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
return await commandProcessor.SendAsync(command, cancellationToken);
});
}
public async Task ExecuteCommand(Func<ICommandProcessor, IMapper, Task> action)
{
await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
var mapper = sp.GetRequiredService<IMapper>();
await action.Invoke(commandProcessor, mapper);
});
}
public async Task ExecuteCommand(Func<ICommandProcessor, Task> action)
{
await ExecuteCommand(async (processor, _) => await action(processor));
}
public async Task<T> ExecuteCommand<T>(Func<ICommandProcessor, Task<T>> action)
{
return await ExecuteCommand(async (processor, _) => await action(processor)).ConfigureAwait(false);
}
public async Task<T> ExecuteCommand<T>(Func<ICommandProcessor, IMapper, Task<T>> action)
{
return await ExecuteScopeAsync(async sp =>
{
var commandProcessor = sp.GetRequiredService<ICommandProcessor>();
var mapper = sp.GetRequiredService<IMapper>();
return await action.Invoke(commandProcessor, mapper);
});
}
public async Task Publish(Func<IBus, Task> action)
{
var bus = _serviceProvider.GetRequiredService<IBus>();
await action(bus);
}
public async Task<TResponse> SendQueryAsync<TResponse>(
IQuery<TResponse> query,
CancellationToken cancellationToken = default)
where TResponse : class
{
return await ExecuteScopeAsync(async sp =>
{
var queryProcessor = sp.GetRequiredService<IQueryProcessor>();
return await queryProcessor.SendAsync(query, cancellationToken);
});
}
public async Task<TResult> ExecuteQuery<TQuery, TResult>(
TQuery query,
CancellationToken cancellationToken = default)
where TQuery : IQuery<TResult>
where TResult : notnull
{
return await ExecuteScopeAsync(async sp =>
{
var queryProcessor = sp.GetRequiredService<IQueryProcessor>();
return await queryProcessor.SendAsync(query, cancellationToken);
});
}
public async Task<T> ExecuteQuery<T>(Func<IQueryProcessor, Task<T>> action)
{
return await ExecuteQuery(async (processor, _) => await action(processor))
.ConfigureAwait(false);
}
public async Task<T> ExecuteQuery<T>(Func<IQueryProcessor, IMapper, Task<T>> action)
{
return await ExecuteScopeAsync(async sp =>
{
var queryProcessor = sp.GetRequiredService<IQueryProcessor>();
var mapper = sp.GetRequiredService<IMapper>();
return await action.Invoke(queryProcessor, mapper);
});
}
}