From 7e3d10a5aec6bae5a69627713476c4e4f82c1d0a Mon Sep 17 00:00:00 2001 From: adimiko Date: Thu, 13 Mar 2025 07:36:39 +0100 Subject: [PATCH 1/2] Moved commands --- source/EasyWay.WebApi/Internals/WebApiResultMapper.cs | 2 +- source/EasyWay/CommandResult.cs | 2 +- .../Internals/Commands/{ => Commands}/CommandExecutor.cs | 2 +- .../Internals/Commands/{ => Commands}/ICommandExecutor.cs | 2 +- .../{Results => CommandsWithResult}/CommandErrorEnum.cs | 2 +- .../CommandWithOperationResultExecutor.cs | 2 +- .../ICommandWithOperationResultExecutor.cs | 2 +- source/EasyWay/Internals/Commands/Extensions.cs | 2 ++ source/EasyWay/Internals/Modules/ModuleExecutor.cs | 3 ++- 9 files changed, 11 insertions(+), 8 deletions(-) rename source/EasyWay/Internals/Commands/{ => Commands}/CommandExecutor.cs (97%) rename source/EasyWay/Internals/Commands/{ => Commands}/ICommandExecutor.cs (83%) rename source/EasyWay/Internals/Commands/{Results => CommandsWithResult}/CommandErrorEnum.cs (70%) rename source/EasyWay/Internals/Commands/{ => CommandsWithResult}/CommandWithOperationResultExecutor.cs (97%) rename source/EasyWay/Internals/Commands/{ => CommandsWithResult}/ICommandWithOperationResultExecutor.cs (86%) diff --git a/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs b/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs index 7948921..8428117 100644 --- a/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs +++ b/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs @@ -1,4 +1,4 @@ -using EasyWay.Internals.Commands.Results; +using EasyWay.Internals.Commands.CommandsWithResult; using EasyWay.Internals.Queries.Results; using Microsoft.AspNetCore.Http; diff --git a/source/EasyWay/CommandResult.cs b/source/EasyWay/CommandResult.cs index f67b9a8..12bacd7 100644 --- a/source/EasyWay/CommandResult.cs +++ b/source/EasyWay/CommandResult.cs @@ -1,4 +1,4 @@ -using EasyWay.Internals.Commands.Results; +using EasyWay.Internals.Commands.CommandsWithResult; namespace EasyWay { diff --git a/source/EasyWay/Internals/Commands/CommandExecutor.cs b/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs similarity index 97% rename from source/EasyWay/Internals/Commands/CommandExecutor.cs rename to source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs index 9118081..579898e 100644 --- a/source/EasyWay/Internals/Commands/CommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs @@ -2,7 +2,7 @@ using EasyWay.Internals.Validation; using Microsoft.Extensions.DependencyInjection; -namespace EasyWay.Internals.Commands +namespace EasyWay.Internals.Commands.Commands { internal sealed class CommandExecutor : ICommandExecutor where TModule : EasyWayModule diff --git a/source/EasyWay/Internals/Commands/ICommandExecutor.cs b/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs similarity index 83% rename from source/EasyWay/Internals/Commands/ICommandExecutor.cs rename to source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs index c6f64b5..048b7dc 100644 --- a/source/EasyWay/Internals/Commands/ICommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs @@ -1,4 +1,4 @@ -namespace EasyWay.Internals.Commands +namespace EasyWay.Internals.Commands.Commands { internal interface ICommandExecutor where TModule : EasyWayModule diff --git a/source/EasyWay/Internals/Commands/Results/CommandErrorEnum.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandErrorEnum.cs similarity index 70% rename from source/EasyWay/Internals/Commands/Results/CommandErrorEnum.cs rename to source/EasyWay/Internals/Commands/CommandsWithResult/CommandErrorEnum.cs index f33f584..4a013f0 100644 --- a/source/EasyWay/Internals/Commands/Results/CommandErrorEnum.cs +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandErrorEnum.cs @@ -1,4 +1,4 @@ -namespace EasyWay.Internals.Commands.Results +namespace EasyWay.Internals.Commands.CommandsWithResult { internal enum CommandErrorEnum { diff --git a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs similarity index 97% rename from source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs rename to source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs index 78957f6..019b4ca 100644 --- a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs @@ -2,7 +2,7 @@ using EasyWay.Internals.Validation; using Microsoft.Extensions.DependencyInjection; -namespace EasyWay.Internals.Commands +namespace EasyWay.Internals.Commands.CommandsWithResult { internal sealed class CommandWithOperationResultExecutor : ICommandWithOperationResultExecutor where TModule : EasyWayModule diff --git a/source/EasyWay/Internals/Commands/ICommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs similarity index 86% rename from source/EasyWay/Internals/Commands/ICommandWithOperationResultExecutor.cs rename to source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs index 6388fb2..6d8f4a5 100644 --- a/source/EasyWay/Internals/Commands/ICommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs @@ -1,4 +1,4 @@ -namespace EasyWay.Internals.Commands +namespace EasyWay.Internals.Commands.CommandsWithResult { internal interface ICommandWithOperationResultExecutor where TModule : EasyWayModule diff --git a/source/EasyWay/Internals/Commands/Extensions.cs b/source/EasyWay/Internals/Commands/Extensions.cs index d64d080..c6e454c 100644 --- a/source/EasyWay/Internals/Commands/Extensions.cs +++ b/source/EasyWay/Internals/Commands/Extensions.cs @@ -1,4 +1,6 @@ using System.Reflection; +using EasyWay.Internals.Commands.Commands; +using EasyWay.Internals.Commands.CommandsWithResult; using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Commands diff --git a/source/EasyWay/Internals/Modules/ModuleExecutor.cs b/source/EasyWay/Internals/Modules/ModuleExecutor.cs index 373cea2..3818ef7 100644 --- a/source/EasyWay/Internals/Modules/ModuleExecutor.cs +++ b/source/EasyWay/Internals/Modules/ModuleExecutor.cs @@ -1,4 +1,5 @@ - using EasyWay.Internals.Commands; +using EasyWay.Internals.Commands.Commands; +using EasyWay.Internals.Commands.CommandsWithResult; using EasyWay.Internals.Queries; using Microsoft.Extensions.DependencyInjection; From 3d1218ca4cd8e1732b58747d1b8edbb4487d7f00 Mon Sep 17 00:00:00 2001 From: adimiko Date: Fri, 14 Mar 2025 07:21:55 +0100 Subject: [PATCH 2/2] Added decorator for Commands and changed decorators for Queries --- .../EntityFrameworkTransaction.cs | 3 +- .../Internals/Transactions/Extensions.cs | 3 +- .../AggregateRoots/ConcurrencyTokenUpdater.cs | 2 +- .../Internals/AggregateRoots/Extensions.cs | 2 +- .../IConcurrencyTokenUpdater.cs | 7 --- .../Commands/Commands/CommandExecutor.cs | 17 ++++--- .../CommandExecutorLoggerDecorator.cs | 45 +++++++++++++++++++ .../Internals/Commands/Commands/Extensions.cs | 23 ++++++++++ .../Commands/Commands/ICommandExecutor.cs | 6 +-- .../CommandWithOperationResultExecutor.cs | 17 ++++--- ...hOperationResultExecutorLoggerDecorator.cs | 25 +++++++++++ .../Commands/CommandsWithResult/Extensions.cs | 20 +++++++++ .../ICommandWithOperationResultExecutor.cs | 6 +-- .../EasyWay/Internals/Commands/Extensions.cs | 7 +-- .../ITransaction.cs | 2 +- .../Commands/IUnitOfWorkCommandHandler.cs | 7 --- ...tOfWorkCommandHandler.cs => UnitOfWork.cs} | 19 ++++---- .../Internals/Modules/ModuleExecutor.cs | 8 ++-- ...eryExecutorCancellationContextDecorator.cs | 29 ------------ .../QueryExecutorValidatorDecorator.cs | 42 ----------------- .../EasyWay/Internals/Queries/Extensions.cs | 10 +---- .../Internals/Queries/QueryExecutor.cs | 21 +++++++-- .../QueryExecutorLoggerDecorator.cs | 6 +-- 23 files changed, 181 insertions(+), 146 deletions(-) delete mode 100644 source/EasyWay/Internals/AggregateRoots/IConcurrencyTokenUpdater.cs create mode 100644 source/EasyWay/Internals/Commands/Commands/CommandExecutorLoggerDecorator.cs create mode 100644 source/EasyWay/Internals/Commands/Commands/Extensions.cs create mode 100644 source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutorLoggerDecorator.cs create mode 100644 source/EasyWay/Internals/Commands/CommandsWithResult/Extensions.cs rename source/EasyWay/Internals/{Transactions => Commands}/ITransaction.cs (63%) delete mode 100644 source/EasyWay/Internals/Commands/IUnitOfWorkCommandHandler.cs rename source/EasyWay/Internals/Commands/{UnitOfWorkCommandHandler.cs => UnitOfWork.cs} (52%) delete mode 100644 source/EasyWay/Internals/Queries/Decorators/QueryExecutorCancellationContextDecorator.cs delete mode 100644 source/EasyWay/Internals/Queries/Decorators/QueryExecutorValidatorDecorator.cs rename source/EasyWay/Internals/Queries/{Decorators => }/QueryExecutorLoggerDecorator.cs (78%) diff --git a/source/EasyWay.EntityFrameworkCore/Internals/Transactions/EntityFrameworkTransaction.cs b/source/EasyWay.EntityFrameworkCore/Internals/Transactions/EntityFrameworkTransaction.cs index e233967..c0d486e 100644 --- a/source/EasyWay.EntityFrameworkCore/Internals/Transactions/EntityFrameworkTransaction.cs +++ b/source/EasyWay.EntityFrameworkCore/Internals/Transactions/EntityFrameworkTransaction.cs @@ -1,4 +1,5 @@ -using EasyWay.Internals.Transactions.Policies; +using EasyWay.Internals.Commands; +using EasyWay.Internals.Transactions.Policies; using Microsoft.EntityFrameworkCore; namespace EasyWay.Internals.Transactions diff --git a/source/EasyWay.EntityFrameworkCore/Internals/Transactions/Extensions.cs b/source/EasyWay.EntityFrameworkCore/Internals/Transactions/Extensions.cs index 8fdf785..7742cfc 100644 --- a/source/EasyWay.EntityFrameworkCore/Internals/Transactions/Extensions.cs +++ b/source/EasyWay.EntityFrameworkCore/Internals/Transactions/Extensions.cs @@ -1,4 +1,5 @@ -using EasyWay.Internals.Transactions.Policies; +using EasyWay.Internals.Commands; +using EasyWay.Internals.Transactions.Policies; using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Transactions diff --git a/source/EasyWay/Internals/AggregateRoots/ConcurrencyTokenUpdater.cs b/source/EasyWay/Internals/AggregateRoots/ConcurrencyTokenUpdater.cs index d7e89a1..f1f9e7f 100644 --- a/source/EasyWay/Internals/AggregateRoots/ConcurrencyTokenUpdater.cs +++ b/source/EasyWay/Internals/AggregateRoots/ConcurrencyTokenUpdater.cs @@ -1,6 +1,6 @@ namespace EasyWay.Internals.AggregateRoots { - internal sealed class ConcurrencyTokenUpdater : IConcurrencyTokenUpdater + internal sealed class ConcurrencyTokenUpdater { private readonly IAggregateRootsContext _aggragateRootsContext; diff --git a/source/EasyWay/Internals/AggregateRoots/Extensions.cs b/source/EasyWay/Internals/AggregateRoots/Extensions.cs index beaa907..9d5f944 100644 --- a/source/EasyWay/Internals/AggregateRoots/Extensions.cs +++ b/source/EasyWay/Internals/AggregateRoots/Extensions.cs @@ -7,7 +7,7 @@ internal static class Extensions internal static IServiceCollection AddAggregateRoots( this IServiceCollection services) { - services.AddScoped(); + services.AddScoped(); return services; } diff --git a/source/EasyWay/Internals/AggregateRoots/IConcurrencyTokenUpdater.cs b/source/EasyWay/Internals/AggregateRoots/IConcurrencyTokenUpdater.cs deleted file mode 100644 index df3127a..0000000 --- a/source/EasyWay/Internals/AggregateRoots/IConcurrencyTokenUpdater.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace EasyWay.Internals.AggregateRoots -{ - internal interface IConcurrencyTokenUpdater - { - void Update(); - } -} diff --git a/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs b/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs index 579898e..0356ba2 100644 --- a/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/Commands/CommandExecutor.cs @@ -1,29 +1,28 @@ -using EasyWay.Internals.Contexts; -using EasyWay.Internals.Validation; +using EasyWay.Internals.Validation; using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Commands.Commands { - internal sealed class CommandExecutor : ICommandExecutor - where TModule : EasyWayModule + internal sealed class CommandExecutor : ICommandExecutor { private readonly IServiceProvider _serviceProvider; private readonly CancellationContext _cancellationContext; - private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler; + private readonly UnitOfWork _unitOfWork; public CommandExecutor( IServiceProvider serviceProvider, CancellationContext cancellationContext, - IUnitOfWorkCommandHandler unitOfWorkCommandHandler) + UnitOfWork unitOfWork) { _serviceProvider = serviceProvider; _cancellationContext = cancellationContext; - _unitOfWorkCommandHandler = unitOfWorkCommandHandler; + _unitOfWork = unitOfWork; } - public async Task Execute(TCommand command, CancellationToken cancellationToken) + public async Task Execute(TCommand command, CancellationToken cancellationToken) + where TModule : EasyWayModule where TCommand : Command { _cancellationContext.Set(cancellationToken); @@ -44,7 +43,7 @@ public async Task Execute(TCommand command, Cancellatio .GetRequiredService>() .Handle(command); - await _unitOfWorkCommandHandler.Handle(); + await _unitOfWork.Commit(); return commandResult; } diff --git a/source/EasyWay/Internals/Commands/Commands/CommandExecutorLoggerDecorator.cs b/source/EasyWay/Internals/Commands/Commands/CommandExecutorLoggerDecorator.cs new file mode 100644 index 0000000..4d8a9ea --- /dev/null +++ b/source/EasyWay/Internals/Commands/Commands/CommandExecutorLoggerDecorator.cs @@ -0,0 +1,45 @@ +using EasyWay.Internals.Queries.Loggers; +using Microsoft.Extensions.DependencyInjection; + +namespace EasyWay.Internals.Commands.Commands +{ + internal sealed class CommandExecutorLoggerDecorator : ICommandExecutor + { + private readonly ICommandExecutor _decoratedCommandExecutor; + + private readonly IServiceProvider _serviceProvider; + + public CommandExecutorLoggerDecorator( + ICommandExecutor decoratedCommandExecutor, + IServiceProvider serviceProvider) + { + _decoratedCommandExecutor = decoratedCommandExecutor; + _serviceProvider = serviceProvider; + } + + public async Task Execute(TCommand command, CancellationToken cancellationToken) + where TModule : EasyWayModule + where TCommand : Command + { + var logger = _serviceProvider.GetRequiredService>(); + + //TODO begin scope (correlation Id) + + logger.Executing(command); + + try + { + var result = await _decoratedCommandExecutor.Execute(command, cancellationToken); + + logger.Executed(); + + return result; + } + catch (Exception ex) + { + logger.UnexpectedException(ex); + throw; + } + } + } +} diff --git a/source/EasyWay/Internals/Commands/Commands/Extensions.cs b/source/EasyWay/Internals/Commands/Commands/Extensions.cs new file mode 100644 index 0000000..6d99992 --- /dev/null +++ b/source/EasyWay/Internals/Commands/Commands/Extensions.cs @@ -0,0 +1,23 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace EasyWay.Internals.Commands.Commands +{ + internal static class Extensions + { + internal static IServiceCollection AddCommandExecutor(this IServiceCollection services) + { + services.AddScoped(); + + services.AddScoped(p => + { + var executor = p.GetRequiredService(); + + var logger = new CommandExecutorLoggerDecorator(executor, p); + + return logger; + }); + + return services; + } + } +} diff --git a/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs b/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs index 048b7dc..35cdcdd 100644 --- a/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/Commands/ICommandExecutor.cs @@ -1,9 +1,9 @@ namespace EasyWay.Internals.Commands.Commands { - internal interface ICommandExecutor - where TModule : EasyWayModule + internal interface ICommandExecutor { - Task Execute(TCommand command, CancellationToken cancellationToken) + Task Execute(TCommand command, CancellationToken cancellationToken) + where TModule : EasyWayModule where TCommand : Command; } } diff --git a/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs index 019b4ca..9ad467e 100644 --- a/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/CommandWithOperationResultExecutor.cs @@ -1,29 +1,28 @@ -using EasyWay.Internals.Contexts; -using EasyWay.Internals.Validation; +using EasyWay.Internals.Validation; using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Commands.CommandsWithResult { - internal sealed class CommandWithOperationResultExecutor : ICommandWithOperationResultExecutor - where TModule : EasyWayModule + internal sealed class CommandWithOperationResultExecutor : ICommandWithOperationResultExecutor { private readonly IServiceProvider _serviceProvider; private readonly CancellationContext _cancellationContext; - private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler; + private readonly UnitOfWork _unitOfWork; public CommandWithOperationResultExecutor( IServiceProvider serviceProvider, CancellationContext cancellationContext, - IUnitOfWorkCommandHandler unitOfWorkCommandHandler) + UnitOfWork unitOfWork) { _serviceProvider = serviceProvider; _cancellationContext = cancellationContext; - _unitOfWorkCommandHandler = unitOfWorkCommandHandler; + _unitOfWork = unitOfWork; } - public async Task> Command(TCommand command, CancellationToken cancellationToken = default) + public async Task> Command(TCommand command, CancellationToken cancellationToken = default) + where TModule : EasyWayModule where TCommand : Command where TOperationResult : OperationResult { @@ -45,7 +44,7 @@ public async Task> Command> Command(TCommand command, CancellationToken cancellationToken = default) + where TModule : EasyWayModule + where TCommand : Command + where TOperationResult : OperationResult + { + return _decoratedCommandExecutor.Command(command, cancellationToken); + } + } +} diff --git a/source/EasyWay/Internals/Commands/CommandsWithResult/Extensions.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/Extensions.cs new file mode 100644 index 0000000..42e4773 --- /dev/null +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/Extensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace EasyWay.Internals.Commands.CommandsWithResult +{ + internal static class Extensions + { + internal static IServiceCollection AddCommandExecutorWithResult(this IServiceCollection services) + { + services.AddScoped(); + + services.AddScoped(p => + { + var executor = p.GetRequiredService(); + + return new CommandWithOperationResultExecutorLoggerDecorator(executor, p); + }); + return services; + } + } +} diff --git a/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs index 6d8f4a5..cc60340 100644 --- a/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandsWithResult/ICommandWithOperationResultExecutor.cs @@ -1,9 +1,9 @@ namespace EasyWay.Internals.Commands.CommandsWithResult { - internal interface ICommandWithOperationResultExecutor - where TModule : EasyWayModule + internal interface ICommandWithOperationResultExecutor { - Task> Command(TCommand command, CancellationToken cancellationToken = default) + Task> Command(TCommand command, CancellationToken cancellationToken = default) + where TModule : EasyWayModule where TCommand : Command where TOperationResult : OperationResult; } diff --git a/source/EasyWay/Internals/Commands/Extensions.cs b/source/EasyWay/Internals/Commands/Extensions.cs index c6e454c..dd1666b 100644 --- a/source/EasyWay/Internals/Commands/Extensions.cs +++ b/source/EasyWay/Internals/Commands/Extensions.cs @@ -12,14 +12,15 @@ internal static IServiceCollection AddCommands( Type moduleType, IEnumerable assemblies) { - services.AddScoped(typeof(ICommandExecutor<>).MakeGenericType(moduleType), typeof(CommandExecutor<>).MakeGenericType(moduleType)); - services.AddScoped(typeof(ICommandWithOperationResultExecutor<>).MakeGenericType(moduleType), typeof(CommandWithOperationResultExecutor<>).MakeGenericType(moduleType)); + services + .AddCommandExecutor() + .AddCommandExecutorWithResult(); services.AddAsBasedType(typeof(CommandHandler<>), ServiceLifetime.Scoped, assemblies); services.AddAsBasedType(typeof(CommandHandler<,>), ServiceLifetime.Scoped, assemblies); - services.AddScoped(); + services.AddScoped(); services.AddSingleton(new ConcurrencyConflictValidator()); diff --git a/source/EasyWay/Internals/Transactions/ITransaction.cs b/source/EasyWay/Internals/Commands/ITransaction.cs similarity index 63% rename from source/EasyWay/Internals/Transactions/ITransaction.cs rename to source/EasyWay/Internals/Commands/ITransaction.cs index 804dca0..f09fb84 100644 --- a/source/EasyWay/Internals/Transactions/ITransaction.cs +++ b/source/EasyWay/Internals/Commands/ITransaction.cs @@ -1,4 +1,4 @@ -namespace EasyWay.Internals.Transactions +namespace EasyWay.Internals.Commands { internal interface ITransaction { diff --git a/source/EasyWay/Internals/Commands/IUnitOfWorkCommandHandler.cs b/source/EasyWay/Internals/Commands/IUnitOfWorkCommandHandler.cs deleted file mode 100644 index ffe342a..0000000 --- a/source/EasyWay/Internals/Commands/IUnitOfWorkCommandHandler.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace EasyWay.Internals.Commands -{ - internal interface IUnitOfWorkCommandHandler - { - Task Handle(); - } -} diff --git a/source/EasyWay/Internals/Commands/UnitOfWorkCommandHandler.cs b/source/EasyWay/Internals/Commands/UnitOfWork.cs similarity index 52% rename from source/EasyWay/Internals/Commands/UnitOfWorkCommandHandler.cs rename to source/EasyWay/Internals/Commands/UnitOfWork.cs index e476718..e407b6e 100644 --- a/source/EasyWay/Internals/Commands/UnitOfWorkCommandHandler.cs +++ b/source/EasyWay/Internals/Commands/UnitOfWork.cs @@ -1,34 +1,33 @@ using EasyWay.Internals.AggregateRoots; using EasyWay.Internals.DomainEvents; -using EasyWay.Internals.Transactions; namespace EasyWay.Internals.Commands { - internal sealed class UnitOfWorkCommandHandler : IUnitOfWorkCommandHandler + internal sealed class UnitOfWork { private readonly IDomainEventContextDispacher _domainEventDispacher; - private readonly IConcurrencyTokenUpdater _concurrencyTokenUpdater; + private readonly ConcurrencyTokenUpdater _concurrencyTokenUpdater; - private readonly ITransaction _unitOfWork; + private readonly ITransaction _transaction; - public UnitOfWorkCommandHandler( + public UnitOfWork( IDomainEventContextDispacher domainEventDispacher, - IConcurrencyTokenUpdater concurrencyTokenUpdater, - ITransaction unitOfWork) + ConcurrencyTokenUpdater concurrencyTokenUpdater, + ITransaction transaction) { _domainEventDispacher = domainEventDispacher; _concurrencyTokenUpdater = concurrencyTokenUpdater; - _unitOfWork = unitOfWork; + _transaction = transaction; } - public async Task Handle() + public async Task Commit() { await _domainEventDispacher.Dispach().ConfigureAwait(false); _concurrencyTokenUpdater.Update(); - await _unitOfWork.Commit().ConfigureAwait(false); + await _transaction.Commit().ConfigureAwait(false); } } } diff --git a/source/EasyWay/Internals/Modules/ModuleExecutor.cs b/source/EasyWay/Internals/Modules/ModuleExecutor.cs index 3818ef7..5e8dd72 100644 --- a/source/EasyWay/Internals/Modules/ModuleExecutor.cs +++ b/source/EasyWay/Internals/Modules/ModuleExecutor.cs @@ -25,8 +25,8 @@ public async Task Command(TCommand command, Cancellatio var sp = scope.ServiceProvider; commandResult = await sp - .GetRequiredService>() - .Execute(command, cancellationToken); + .GetRequiredService() + .Execute(command, cancellationToken); } return commandResult; @@ -43,8 +43,8 @@ public async Task> Command>() - .Command(command, cancellationToken); + .GetRequiredService() + .Command(command, cancellationToken); } return commandResult; diff --git a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorCancellationContextDecorator.cs b/source/EasyWay/Internals/Queries/Decorators/QueryExecutorCancellationContextDecorator.cs deleted file mode 100644 index e69cd3e..0000000 --- a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorCancellationContextDecorator.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; - -namespace EasyWay.Internals.Queries.Decorators -{ - internal sealed class QueryExecutorCancellationContextDecorator : IQueryExecutor - { - private readonly IQueryExecutor _decoratedQueryExecutor; - - private readonly IServiceProvider _serviceProvider; - - public QueryExecutorCancellationContextDecorator( - IQueryExecutor decoratedQueryExecutor, - IServiceProvider serviceProvider) - { - _decoratedQueryExecutor = decoratedQueryExecutor; - _serviceProvider = serviceProvider; - } - - public Task> Execute(TQuery query, CancellationToken cancellationToken = default) - where TModule : EasyWayModule - where TQuery : Query - where TReadModel : ReadModel - { - _serviceProvider.GetRequiredService().Set(cancellationToken); - - return _decoratedQueryExecutor.Execute(query, cancellationToken); - } - } -} diff --git a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorValidatorDecorator.cs b/source/EasyWay/Internals/Queries/Decorators/QueryExecutorValidatorDecorator.cs deleted file mode 100644 index 867fa93..0000000 --- a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorValidatorDecorator.cs +++ /dev/null @@ -1,42 +0,0 @@ -using EasyWay.Internals.Validation; -using Microsoft.Extensions.DependencyInjection; - -namespace EasyWay.Internals.Queries.Decorators -{ - internal sealed class QueryExecutorValidatorDecorator : IQueryExecutor - { - private readonly IQueryExecutor _decoratedQueryExecutor; - - private readonly IServiceProvider _serviceProvider; - - public QueryExecutorValidatorDecorator( - IQueryExecutor decoratedQueryExecutor, - IServiceProvider serviceProvider) - { - _decoratedQueryExecutor = decoratedQueryExecutor; - _serviceProvider = serviceProvider; - } - - public Task> Execute(TQuery query, CancellationToken cancellationToken = default) - where TModule : EasyWayModule - where TQuery : Query - where TReadModel : ReadModel - { - var validatorType = typeof(IEasyWayValidator<>).MakeGenericType(query.GetType()); - - var validator = _serviceProvider.GetService>(); - - if (validator is not null) - { - var errors = validator.Validate(query); - - if (errors.Any()) - { - return Task.FromResult(QueryResult.Validation(errors)); - } - } - - return _decoratedQueryExecutor.Execute(query, cancellationToken); - } - } -} diff --git a/source/EasyWay/Internals/Queries/Extensions.cs b/source/EasyWay/Internals/Queries/Extensions.cs index 8b248dd..daa926e 100644 --- a/source/EasyWay/Internals/Queries/Extensions.cs +++ b/source/EasyWay/Internals/Queries/Extensions.cs @@ -1,6 +1,4 @@ using System.Reflection; -using EasyWay.Internals.Queries.Decorators; -using EasyWay.Internals.Validation; using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Queries @@ -17,13 +15,7 @@ internal static IServiceCollection AddQueries( { var executor = p.GetRequiredService(); - var cancellationContext = new QueryExecutorCancellationContextDecorator(executor, p); - - var validator = new QueryExecutorValidatorDecorator(cancellationContext, p); - - var logger = new QueryExecutorLoggerDecorator(validator, p); - - return logger; + return new QueryExecutorLoggerDecorator(executor, p); }); services.AddAsBasedType(typeof(QueryHandler<,>), ServiceLifetime.Scoped, assemblies); diff --git a/source/EasyWay/Internals/Queries/QueryExecutor.cs b/source/EasyWay/Internals/Queries/QueryExecutor.cs index 942892e..d476259 100644 --- a/source/EasyWay/Internals/Queries/QueryExecutor.cs +++ b/source/EasyWay/Internals/Queries/QueryExecutor.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using EasyWay.Internals.Validation; +using Microsoft.Extensions.DependencyInjection; namespace EasyWay.Internals.Queries { @@ -11,12 +12,26 @@ public QueryExecutor(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; } - public async Task> Execute(TQuery query, CancellationToken cancellationToken = default) + public Task> Execute(TQuery query, CancellationToken cancellationToken = default) where TModule : EasyWayModule where TQuery : Query where TReadModel : ReadModel { - return await _serviceProvider.GetRequiredService>().Handle(query); + _serviceProvider.GetRequiredService().Set(cancellationToken); + + var validator = _serviceProvider.GetService>(); + + if (validator is not null) + { + var errors = validator.Validate(query); + + if (errors.Any()) + { + return Task.FromResult(QueryResult.Validation(errors)); + } + } + + return _serviceProvider.GetRequiredService>().Handle(query); } } } diff --git a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorLoggerDecorator.cs b/source/EasyWay/Internals/Queries/QueryExecutorLoggerDecorator.cs similarity index 78% rename from source/EasyWay/Internals/Queries/Decorators/QueryExecutorLoggerDecorator.cs rename to source/EasyWay/Internals/Queries/QueryExecutorLoggerDecorator.cs index e5be751..0af0736 100644 --- a/source/EasyWay/Internals/Queries/Decorators/QueryExecutorLoggerDecorator.cs +++ b/source/EasyWay/Internals/Queries/QueryExecutorLoggerDecorator.cs @@ -1,7 +1,7 @@ using EasyWay.Internals.Queries.Loggers; using Microsoft.Extensions.DependencyInjection; -namespace EasyWay.Internals.Queries.Decorators +namespace EasyWay.Internals.Queries { internal sealed class QueryExecutorLoggerDecorator : IQueryExecutor { @@ -17,7 +17,7 @@ public QueryExecutorLoggerDecorator( _serviceProvider = serviceProvider; } - public Task> Execute(TQuery query, CancellationToken cancellationToken = default) + public async Task> Execute(TQuery query, CancellationToken cancellationToken = default) where TModule : EasyWayModule where TQuery : Query where TReadModel : ReadModel @@ -30,7 +30,7 @@ public Task> Execute(TQuery try { - var result = _decoratedQueryExecutor.Execute(query, cancellationToken); + var result = await _decoratedQueryExecutor.Execute(query, cancellationToken); logger.Executed();