-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed contract for ModuleExecutor (#55)
* Changed contract for command * Changed contract for query
- Loading branch information
Showing
10 changed files
with
108 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using EasyWay.Internals.Contexts; | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace EasyWay.Internals.Commands | ||
{ | ||
internal sealed class CommandWithOperationResultExecutor<TModule> : ICommandWithOperationResultExecutor<TModule> | ||
where TModule : EasyWayModule | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
private readonly ICancellationContextConstructor _cancellationContextConstructor; | ||
|
||
private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler; | ||
|
||
public CommandWithOperationResultExecutor( | ||
IServiceProvider serviceProvider, | ||
ICancellationContextConstructor cancellationContextConstructor, | ||
IUnitOfWorkCommandHandler unitOfWorkCommandHandler) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_cancellationContextConstructor = cancellationContextConstructor; | ||
_unitOfWorkCommandHandler = unitOfWorkCommandHandler; | ||
} | ||
|
||
public async Task<CommandResult<TOperationResult>> Execute<TOperationResult>(Command<TModule, TOperationResult> command, CancellationToken cancellationToken) | ||
where TOperationResult : OperationResult | ||
{ | ||
_cancellationContextConstructor.Set(cancellationToken); | ||
|
||
var commandType = command.GetType(); | ||
|
||
var validatorType = typeof(IValidator<>).MakeGenericType(commandType); | ||
|
||
var validator = _serviceProvider.GetService(validatorType); | ||
|
||
if (validator is not null) | ||
{ | ||
var result = (ValidationResult)validatorType | ||
.GetMethod("Validate") | ||
?.Invoke(validator, [command]); | ||
|
||
if (!result.IsValid) | ||
{ | ||
var errors = result.Errors | ||
.GroupBy(x => x.PropertyName) | ||
.ToDictionary( | ||
g => g.Key, | ||
g => g.Select(x => x.ErrorCode).ToArray() | ||
); | ||
|
||
return CommandResult<TOperationResult>.Validation(errors); | ||
} | ||
} | ||
|
||
var commandHandlerType = typeof(ICommandHandler<,,>).MakeGenericType(typeof(TModule), commandType, typeof(TOperationResult)); | ||
|
||
var commandHandler = _serviceProvider.GetRequiredService(commandHandlerType); | ||
|
||
var commandResult = await (Task<CommandResult<TOperationResult>>)commandHandlerType.GetMethod("Handle").Invoke(commandHandler, [command]); | ||
|
||
await _unitOfWorkCommandHandler.Handle(); | ||
|
||
return commandResult; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
source/EasyWay/Internals/Commands/ICommandWithOperationResultExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace EasyWay.Internals.Commands | ||
{ | ||
internal interface ICommandWithOperationResultExecutor<TModule> | ||
where TModule : EasyWayModule | ||
{ | ||
Task<CommandResult<TOperationResult>> Execute<TOperationResult>(Command<TModule, TOperationResult> command, CancellationToken cancellationToken) | ||
where TOperationResult : OperationResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters