Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion source/EasyWay.WebApi/Internals/WebApiResultMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EasyWay.Internals.Commands.Results;
using EasyWay.Internals.Commands.CommandsWithResult;
using EasyWay.Internals.Queries.Results;
using Microsoft.AspNetCore.Http;

Expand Down
2 changes: 1 addition & 1 deletion source/EasyWay/CommandResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EasyWay.Internals.Commands.Results;
using EasyWay.Internals.Commands.CommandsWithResult;

namespace EasyWay
{
Expand Down Expand Up @@ -41,7 +41,7 @@
ValidationErrors = new Dictionary<string, string[]>();
}

private CommandResult(IDictionary<string, string[]> validationErrors)

Check warning on line 44 in source/EasyWay/CommandResult.cs

View workflow job for this annotation

GitHub Actions / Build & Tests

Non-nullable property 'OperationResult' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
Error = CommandErrorEnum.Validation;
ValidationErrors = validationErrors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace EasyWay.Internals.AggregateRoots
{
internal sealed class ConcurrencyTokenUpdater : IConcurrencyTokenUpdater
internal sealed class ConcurrencyTokenUpdater
{
private readonly IAggregateRootsContext _aggragateRootsContext;

Expand Down
2 changes: 1 addition & 1 deletion source/EasyWay/Internals/AggregateRoots/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class Extensions
internal static IServiceCollection AddAggregateRoots(
this IServiceCollection services)
{
services.AddScoped<IConcurrencyTokenUpdater, ConcurrencyTokenUpdater>();
services.AddScoped<ConcurrencyTokenUpdater>();

return services;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using EasyWay.Internals.Contexts;
using EasyWay.Internals.Validation;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Commands
namespace EasyWay.Internals.Commands.Commands
{
internal sealed class CommandExecutor<TModule> : ICommandExecutor<TModule>
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<CommandResult> Execute<TCommand>(TCommand command, CancellationToken cancellationToken)
public async Task<CommandResult> Execute<TModule, TCommand>(TCommand command, CancellationToken cancellationToken)
where TModule : EasyWayModule
where TCommand : Command
{
_cancellationContext.Set(cancellationToken);
Expand All @@ -44,7 +43,7 @@ public async Task<CommandResult> Execute<TCommand>(TCommand command, Cancellatio
.GetRequiredService<CommandHandler<TCommand>>()
.Handle(command);

await _unitOfWorkCommandHandler.Handle();
await _unitOfWork.Commit();

return commandResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandResult> Execute<TModule, TCommand>(TCommand command, CancellationToken cancellationToken)
where TModule : EasyWayModule
where TCommand : Command
{
var logger = _serviceProvider.GetRequiredService<EasyWayLogger<TModule>>();

//TODO begin scope (correlation Id)

logger.Executing(command);

try
{
var result = await _decoratedCommandExecutor.Execute<TModule, TCommand>(command, cancellationToken);

logger.Executed();

return result;
}
catch (Exception ex)
{
logger.UnexpectedException(ex);
throw;
}
}
}
}
23 changes: 23 additions & 0 deletions source/EasyWay/Internals/Commands/Commands/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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<CommandExecutor>();

services.AddScoped<ICommandExecutor>(p =>
{
var executor = p.GetRequiredService<CommandExecutor>();

var logger = new CommandExecutorLoggerDecorator(executor, p);

return logger;
});

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EasyWay.Internals.Commands.Commands
{
internal interface ICommandExecutor
{
Task<CommandResult> Execute<TModule, TCommand>(TCommand command, CancellationToken cancellationToken)
where TModule : EasyWayModule
where TCommand : Command;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EasyWay.Internals.Commands.Results
namespace EasyWay.Internals.Commands.CommandsWithResult
{
internal enum CommandErrorEnum
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using EasyWay.Internals.Contexts;
using EasyWay.Internals.Validation;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Commands
namespace EasyWay.Internals.Commands.CommandsWithResult
{
internal sealed class CommandWithOperationResultExecutor<TModule> : ICommandWithOperationResultExecutor<TModule>
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<CommandResult<TOperationResult>> Command<TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
public async Task<CommandResult<TOperationResult>> Command<TModule, TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TModule : EasyWayModule
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult
{
Expand All @@ -45,7 +44,7 @@ public async Task<CommandResult<TOperationResult>> Command<TCommand, TOperationR

var commandResult = await commandHandler.Handle(command);

await _unitOfWorkCommandHandler.Handle();
await _unitOfWork.Commit();

return commandResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace EasyWay.Internals.Commands.CommandsWithResult
{
internal sealed class CommandWithOperationResultExecutorLoggerDecorator : ICommandWithOperationResultExecutor
{
private readonly ICommandWithOperationResultExecutor _decoratedCommandExecutor;

private readonly IServiceProvider _serviceProvider;

public CommandWithOperationResultExecutorLoggerDecorator(
ICommandWithOperationResultExecutor decoratedCommandExecutor,
IServiceProvider serviceProvider)
{
_decoratedCommandExecutor = decoratedCommandExecutor;
_serviceProvider = serviceProvider;
}

public Task<CommandResult<TOperationResult>> Command<TModule, TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TModule : EasyWayModule
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult
{
return _decoratedCommandExecutor.Command<TModule, TCommand, TOperationResult>(command, cancellationToken);
}
}
}
20 changes: 20 additions & 0 deletions source/EasyWay/Internals/Commands/CommandsWithResult/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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<CommandWithOperationResultExecutor>();

services.AddScoped<ICommandWithOperationResultExecutor>(p =>
{
var executor = p.GetRequiredService<CommandWithOperationResultExecutor>();

return new CommandWithOperationResultExecutorLoggerDecorator(executor, p);
});
return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EasyWay.Internals.Commands.CommandsWithResult
{
internal interface ICommandWithOperationResultExecutor
{
Task<CommandResult<TOperationResult>> Command<TModule, TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TModule : EasyWayModule
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult;
}
}
9 changes: 6 additions & 3 deletions source/EasyWay/Internals/Commands/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,14 +12,15 @@ internal static IServiceCollection AddCommands(
Type moduleType,
IEnumerable<Assembly> 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<IUnitOfWorkCommandHandler, UnitOfWorkCommandHandler>();
services.AddScoped<UnitOfWork>();

services.AddSingleton(new ConcurrencyConflictValidator());

Expand Down
9 changes: 0 additions & 9 deletions source/EasyWay/Internals/Commands/ICommandExecutor.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EasyWay.Internals.Transactions
namespace EasyWay.Internals.Commands
{
internal interface ITransaction
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading
Loading