1
1
// Copyright (C) Ubiquitous AS. All rights reserved
2
2
// Licensed under the Apache License, Version 2.0.
3
3
4
- using Eventuous . Tools ;
5
- using static Eventuous . Diagnostics . ApplicationEventSource ;
6
-
7
- // ReSharper disable MemberCanBePrivate.Global
8
-
9
4
namespace Eventuous ;
10
5
6
+ using Tools ;
7
+ using static Diagnostics . ApplicationEventSource ;
8
+
11
9
/// <summary>
12
- /// Application service base class. A derived class should be scoped to handle commands for one aggregate type only.
10
+ /// Command service base class. A derived class should be scoped to handle commands for one aggregate type only.
13
11
/// </summary>
14
12
/// <typeparam name="TAggregate">The aggregate type</typeparam>
15
13
/// <typeparam name="TState">The aggregate state type</typeparam>
16
14
/// <typeparam name="TId">The aggregate identity type</typeparam>
17
15
// [PublicAPI]
18
- public abstract class ApplicationService < TAggregate , TState , TId >
19
- : IApplicationService < TAggregate , TState , TId > , IApplicationService < TAggregate >
16
+ public abstract class CommandService < TAggregate , TState , TId >
17
+ : ICommandService < TAggregate , TState , TId > , ICommandService < TAggregate >
20
18
where TAggregate : Aggregate < TState > , new ( )
21
19
where TState : State < TState > , new ( )
22
20
where TId : AggregateId {
21
+ [ PublicAPI ]
23
22
protected IAggregateStore Store { get ; }
24
23
25
24
readonly HandlersMap < TAggregate > _handlers = new ( ) ;
@@ -28,7 +27,7 @@ public abstract class ApplicationService<TAggregate, TState, TId>
28
27
readonly StreamNameMap _streamNameMap ;
29
28
readonly TypeMapper _typeMap ;
30
29
31
- protected ApplicationService (
30
+ protected CommandService (
32
31
IAggregateStore store ,
33
32
AggregateFactoryRegistry ? factoryRegistry = null ,
34
33
StreamNameMap ? streamNameMap = null ,
@@ -196,19 +195,17 @@ protected void OnAsync<TCommand>(ArbitraryActAsync<TCommand> action)
196
195
/// <exception cref="Exceptions.CommandHandlerNotFound{TCommand}"></exception>
197
196
public async Task < Result < TState > > Handle < TCommand > ( TCommand command , CancellationToken cancellationToken )
198
197
where TCommand : class {
199
- var commandType = Ensure . NotNull ( command ) . GetType ( ) ;
200
-
201
- if ( ! _handlers . TryGetValue ( commandType , out var registeredHandler ) ) {
202
- Log . CommandHandlerNotFound ( commandType ) ;
203
- var exception = new Exceptions . CommandHandlerNotFound ( commandType ) ;
198
+ if ( ! _handlers . TryGet < TCommand > ( out var registeredHandler ) ) {
199
+ Log . CommandHandlerNotFound < TCommand > ( ) ;
200
+ var exception = new Exceptions . CommandHandlerNotFound < TCommand > ( ) ;
204
201
return new ErrorResult < TState > ( exception ) ;
205
202
}
206
203
207
- var hasGetIdFunction = _idMap . TryGetValue ( commandType , out var getId ) ;
204
+ var hasGetIdFunction = _idMap . TryGet < TCommand > ( out var getId ) ;
208
205
209
206
if ( ! hasGetIdFunction || getId == null ) {
210
- Log . CannotCalculateAggregateId ( commandType ) ;
211
- var exception = new Exceptions . CommandHandlerNotFound ( commandType ) ;
207
+ Log . CannotCalculateAggregateId < TCommand > ( ) ;
208
+ var exception = new Exceptions . CommandHandlerNotFound < TCommand > ( ) ;
212
209
return new ErrorResult < TState > ( exception ) ;
213
210
}
214
211
@@ -238,37 +235,41 @@ public async Task<Result<TState>> Handle<TCommand>(TCommand command, Cancellatio
238
235
if ( result . Changes . Count == 0 ) return new OkResult < TState > ( result . State , Array . Empty < Change > ( ) , 0 ) ;
239
236
240
237
var storeResult = await Store . Store (
241
- streamName != default ? streamName : GetAggregateStreamName ( ) ,
238
+ streamName != default
239
+ ? streamName
240
+ : GetAggregateStreamName ( ) ,
242
241
result ,
243
242
cancellationToken
244
243
)
245
244
. NoContext ( ) ;
246
245
247
246
var changes = result . Changes . Select ( x => new Change ( x , _typeMap . GetTypeName ( x ) ) ) ;
248
247
249
- Log . CommandHandled ( commandType ) ;
248
+ Log . CommandHandled < TCommand > ( ) ;
250
249
251
250
return new OkResult < TState > ( result . State , changes , storeResult . GlobalPosition ) ;
252
251
}
253
252
catch ( Exception e ) {
254
- Log . ErrorHandlingCommand ( commandType , e ) ;
253
+ Log . ErrorHandlingCommand < TCommand > ( e ) ;
255
254
256
- return new ErrorResult < TState > ( $ "Error handling command { commandType . Name } ", e ) ;
255
+ return new ErrorResult < TState > ( $ "Error handling command { typeof ( TCommand ) . Name } ", e ) ;
257
256
}
258
257
259
- TAggregate Create ( ) => _factoryRegistry . CreateInstance < TAggregate , TState > ( ) ;
258
+ TAggregate Create ( )
259
+ => _factoryRegistry . CreateInstance < TAggregate , TState > ( ) ;
260
260
261
- StreamName GetAggregateStreamName ( ) => _streamNameMap . GetStreamName < TAggregate , TId > ( aggregateId ) ;
261
+ StreamName GetAggregateStreamName ( )
262
+ => _streamNameMap . GetStreamName < TAggregate , TId > ( aggregateId ) ;
262
263
}
263
264
264
- async Task < Result > IApplicationService . Handle < TCommand > ( TCommand command , CancellationToken cancellationToken )
265
+ async Task < Result > ICommandService . Handle < TCommand > ( TCommand command , CancellationToken cancellationToken )
265
266
where TCommand : class {
266
267
var result = await Handle ( command , cancellationToken ) . NoContext ( ) ;
267
268
268
269
return result switch {
269
- OkResult < TState > ( var aggregateState , var enumerable , _ ) => new OkResult ( aggregateState , enumerable ) ,
270
- ErrorResult < TState > error => new ErrorResult ( error . Message , error . Exception ) ,
271
- _ => throw new ApplicationException ( "Unknown result type" )
270
+ OkResult < TState > ( var state , var enumerable , _ ) => new OkResult ( state , enumerable ) ,
271
+ ErrorResult < TState > error => new ErrorResult ( error . Message , error . Exception ) ,
272
+ _ => throw new ApplicationException ( "Unknown result type" )
272
273
} ;
273
274
}
274
275
0 commit comments