-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
CommandHandler.cs
175 lines (153 loc) · 6.21 KB
/
CommandHandler.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
170
171
172
173
174
175
using Core.OptimisticConcurrency;
using ECommerce.Core.EventStoreDB;
using EventStore.Client;
using Microsoft.Extensions.DependencyInjection;
namespace ECommerce.Core.Commands;
public static class CommandHandlerExtensions
{
public static Task<ulong> HandleCreateCommand<TCommand>(EventStoreClient eventStore,
Func<TCommand, object> handle,
Func<TCommand, string> getId,
TCommand command,
CancellationToken ct)
{
var entityId = getId(command);
var @event = handle(command);
return eventStore.Append(entityId, @event, ct);
}
public static async Task<ulong> HandleUpdateCommand<TCommand, TEntity>(
EventStoreClient eventStore,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when,
Func<TCommand, TEntity, object> handle,
Func<TCommand, string> getId,
TCommand command,
ulong expectedVersion,
CancellationToken ct) where TEntity : notnull
{
var id = getId(command);
var entity = await eventStore.Find(getDefault, when, id, ct);
var @event = handle(command, entity);
return await eventStore.Append(id, @event, expectedVersion, ct);
}
public static IServiceCollection AddCreateCommandHandler<TCommand>(
this IServiceCollection services,
Func<TCommand, object> handle,
Func<TCommand, string> getId,
Action<ulong>? setNextExpectedVersion = null
) =>
services.AddCreateCommandHandler(_ => handle, getId, setNextExpectedVersion);
public static IServiceCollection AddCreateCommandHandler<TCommand>(
this IServiceCollection services,
Func<IServiceProvider, Func<TCommand, object>> handle,
Func<TCommand, string> getId
) =>
services
.AddScoped<Func<TCommand, CancellationToken, ValueTask>>(sp =>
{
var eventStore = sp.GetRequiredService<EventStoreClient>();
var nextStreamRevisionProvider =
sp.GetRequiredService<INextResourceVersionProvider>();
return async (command, ct) =>
{
var nextRevision = await HandleCreateCommand(eventStore, handle(sp), getId, command, ct);
nextStreamRevisionProvider.TrySet(nextRevision.ToString());
};
});
public static IServiceCollection AddUpdateCommandHandler<TCommand, TEntity>(
this IServiceCollection services,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when,
Func<TCommand, TEntity, object> handle,
Func<TCommand, string> getId
) where TEntity : notnull =>
services.AddUpdateCommandHandler(getDefault, when, _ => handle, getId);
public static IServiceCollection AddUpdateCommandHandler<TCommand, TEntity>(
this IServiceCollection services,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when,
Func<IServiceProvider, Func<TCommand, TEntity, object>> handle,
Func<TCommand, string> getId
) where TEntity : notnull =>
services
.AddScoped<Func<TCommand, CancellationToken, ValueTask>>(sp =>
{
var eventStoreClient = sp.GetRequiredService<EventStoreClient>();
var expectedStreamRevisionProvider =
sp.GetRequiredService<IExpectedResourceVersionProvider>();
var nextStreamRevisionProvider =
sp.GetRequiredService<INextResourceVersionProvider>();
ulong GetExpectedVersion()
{
var value = expectedStreamRevisionProvider.Value;
if (string.IsNullOrWhiteSpace(value) || !ulong.TryParse(value, out var expectedRevision))
throw new ArgumentNullException("ETag", "Expected version not set");
return expectedRevision;
}
return async (command, ct) =>
{
var nextRevision = await HandleUpdateCommand(
eventStoreClient,
getDefault,
when,
handle(sp),
getId,
command,
GetExpectedVersion(),
ct
);
nextStreamRevisionProvider.TrySet(nextRevision.ToString());
};
});
public static IServiceCollection For<TEntity>(
this IServiceCollection services,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when,
Action<CommandHandlersBuilder<TEntity>> setup
)
where TEntity : class
{
setup(new CommandHandlersBuilder<TEntity>(services, getDefault, when));
return services;
}
public class CommandHandlersBuilder<TEntity>(
IServiceCollection services,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when)
where TEntity : class
{
public readonly IServiceCollection services = services;
public CommandHandlersBuilder<TEntity> AddOn<TCommand>(
Func<TCommand, object> handle,
Func<TCommand, string> getId
)
{
services.AddCreateCommandHandler(_ => handle, getId);
return this;
}
public CommandHandlersBuilder<TEntity> AddOn<TCommand>(
Func<IServiceProvider, Func<TCommand, object>> handle,
Func<TCommand, string> getId
)
{
services.AddCreateCommandHandler(_ => handle, getId);
return this;
}
public CommandHandlersBuilder<TEntity> UpdateOn<TCommand>(
Func<TCommand, TEntity, object> handle,
Func<TCommand, string> getId
)
{
services.AddUpdateCommandHandler(getDefault, when, _ => handle, getId);
return this;
}
public CommandHandlersBuilder<TEntity> UpdateOn<TCommand>(
Func<IServiceProvider, Func<TCommand, TEntity, object>> handle,
Func<TCommand, string> getId
)
{
services.AddUpdateCommandHandler(getDefault, when, handle, getId);
return this;
}
}
}