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
4 changes: 2 additions & 2 deletions samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace EasyWay.Samples.Commands
{
public class ErrorCommandHandler : ICommandHandler<ErrorCommand>
public sealed class ErrorCommandHandler : CommandHandler<ErrorCommand>
{
public Task<CommandResult> Handle(ErrorCommand command)
public sealed override Task<CommandResult> Handle(ErrorCommand command)
{
var x = new SampleAggregateRoot();

Expand Down
8 changes: 4 additions & 4 deletions samples/EasyWay.Samples/Commands/SampleCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace EasyWay.Samples.Commands
{
internal sealed class SampleCommandHandler : ICommandHandler<SampleCommand>
internal sealed class SampleCommandHandler : CommandHandler<SampleCommand>
{
private readonly ICancellationContext _cancellationContext;
private readonly CancellationContext _cancellationContext;

private readonly ISampleAggragateRootRepository _repository;

Expand All @@ -20,7 +20,7 @@ internal sealed class SampleCommandHandler : ICommandHandler<SampleCommand>
private readonly IUserContext _userContext;

public SampleCommandHandler(
ICancellationContext cancellationContext,
CancellationContext cancellationContext,
ISampleAggragateRootRepository repository,
SampleAggregateRootFactory factory,
SampleDomainService domainService,
Expand All @@ -37,7 +37,7 @@ public SampleCommandHandler(
_userContext = userContext;
}

public async Task<CommandResult> Handle(SampleCommand command)
public sealed override async Task<CommandResult> Handle(SampleCommand command)
{
var userId = _userContext.UserId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

namespace EasyWay.Samples.Commands.WithResult
{
public sealed class SampleCommandWithResultHandler : ICommandHandler<SampleCommandWithResult, SampleCommandResult>
public sealed class SampleCommandWithResultHandler : CommandHandler<SampleCommandWithResult, SampleCommandResult>
{
public Task<CommandResult<SampleCommandResult>> Handle(SampleCommandWithResult command)
public sealed override Task<CommandResult<SampleCommandResult>> Handle(SampleCommandWithResult command)
{
return Task.FromResult(CommandResult<SampleCommandResult>.Ok(new SampleCommandResult()));
}
Expand Down
4 changes: 2 additions & 2 deletions samples/EasyWay.Samples/Events/SampleEventHandler1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace EasyWay.Samples.Events
{
public class SampleEventHandler1 : IDomainEventHandler<CreatedSampleAggragete>
public sealed class SampleEventHandler1 : DomainEventHandler<CreatedSampleAggragete>
{
public Task Handle(CreatedSampleAggragete @event)
public sealed override Task Handle(CreatedSampleAggragete domainEvent)
{
return Task.CompletedTask;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/EasyWay.Samples/Events/SampleEventHandler2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace EasyWay.Samples.Events
{
public class SampleEventHandler2 : IDomainEventHandler<CreatedSampleAggragete>
public sealed class SampleEventHandler2 : DomainEventHandler<CreatedSampleAggragete>
{
public Task Handle(CreatedSampleAggragete @event)
public sealed override Task Handle(CreatedSampleAggragete domainEvent)
{
return Task.CompletedTask;
}
Expand Down
5 changes: 4 additions & 1 deletion samples/EasyWay.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EasyWay.Samples;
using EasyWay.Samples.Commands;
using EasyWay.Samples.Commands.WithResult;
using EasyWay.Samples.Domain.Policies;
using EasyWay.Samples.Queries;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -40,7 +41,9 @@ await kernel

app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
{
return await executor.Command(command);
var x = await executor.Command(command);

return mapper.Map(x);
});

app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
Expand Down
6 changes: 2 additions & 4 deletions samples/EasyWay.Samples/Queries/SampleQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace EasyWay.Samples.Queries
{
public class SampleQueryHandler : IQueryHandler<SampleQuery, SampleQueryResult>
internal sealed class SampleQueryHandler : QueryHandler<SampleQuery, SampleQueryResult>
{
public Task<QueryResult<SampleQueryResult>> Handle(SampleQuery query)
public sealed override Task<QueryResult<SampleQueryResult>> Handle(SampleQuery query)
{
//return Task.FromResult(QueryResult<SampleQueryResult>.Forbidden);

return Task.FromResult(QueryResult<SampleQueryResult>.Ok(new SampleQueryResult()));
}
}
Expand Down
9 changes: 9 additions & 0 deletions source/EasyWay/CancellationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EasyWay
{
public sealed class CancellationContext
{
public CancellationToken Token { get; private set; }

internal void Set(CancellationToken token) => Token = token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
/// Defines a handler for a command
/// </summary>
/// <typeparam name="TCommand">The type of command being handled</typeparam>
public interface ICommandHandler<TCommand>
public abstract class CommandHandler<TCommand>
where TCommand : Command
{
/// <summary>
/// Handles a command
/// </summary>
/// <param name="command">Command</param>
Task<CommandResult> Handle(TCommand command);
public abstract Task<CommandResult> Handle(TCommand command);
}

public interface ICommandHandler<TCommand, TOperationResult>
public abstract class CommandHandler<TCommand, TOperationResult>
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult
{
Task<CommandResult<TOperationResult>> Handle(TCommand command);
public abstract Task<CommandResult<TOperationResult>> Handle(TCommand command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
/// Defines a handler for an event
/// </summary>
/// <typeparam name="TDomainEvent">The type of event being handled</typeparam>
public interface IDomainEventHandler<TDomainEvent>
public abstract class DomainEventHandler<TDomainEvent>
where TDomainEvent : DomainEvent
{
/// <summary>
/// Handles an event
/// </summary>
/// <param name="domainEvent">Event</param>
Task Handle(TDomainEvent domainEvent);
public abstract Task Handle(TDomainEvent domainEvent);
}
}
7 changes: 0 additions & 7 deletions source/EasyWay/ICancellationContext.cs

This file was deleted.

10 changes: 5 additions & 5 deletions source/EasyWay/Internals/Commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ internal sealed class CommandExecutor<TModule> : ICommandExecutor<TModule>
{
private readonly IServiceProvider _serviceProvider;

private readonly ICancellationContextConstructor _cancellationContextConstructor;
private readonly CancellationContext _cancellationContext;

private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler;

public CommandExecutor(
IServiceProvider serviceProvider,
ICancellationContextConstructor cancellationContextConstructor,
CancellationContext cancellationContext,
IUnitOfWorkCommandHandler unitOfWorkCommandHandler)
{
_serviceProvider = serviceProvider;
_cancellationContextConstructor = cancellationContextConstructor;
_cancellationContext = cancellationContext;
_unitOfWorkCommandHandler = unitOfWorkCommandHandler;
}

public async Task<CommandResult> Execute<TCommand>(TCommand command, CancellationToken cancellationToken)
where TCommand : Command
{
_cancellationContextConstructor.Set(cancellationToken);
_cancellationContext.Set(cancellationToken);

var validator = _serviceProvider.GetService<IEasyWayValidator<TCommand>>();

Expand All @@ -41,7 +41,7 @@ public async Task<CommandResult> Execute<TCommand>(TCommand command, Cancellatio
}

var commandResult = await _serviceProvider
.GetRequiredService<ICommandHandler<TCommand>>()
.GetRequiredService<CommandHandler<TCommand>>()
.Handle(command);

await _unitOfWorkCommandHandler.Handle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ internal sealed class CommandWithOperationResultExecutor<TModule> : ICommandWith
{
private readonly IServiceProvider _serviceProvider;

private readonly ICancellationContextConstructor _cancellationContextConstructor;
private readonly CancellationContext _cancellationContext;

private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler;

public CommandWithOperationResultExecutor(
IServiceProvider serviceProvider,
ICancellationContextConstructor cancellationContextConstructor,
CancellationContext cancellationContext,
IUnitOfWorkCommandHandler unitOfWorkCommandHandler)
{
_serviceProvider = serviceProvider;
_cancellationContextConstructor = cancellationContextConstructor;
_cancellationContext = cancellationContext;
_unitOfWorkCommandHandler = unitOfWorkCommandHandler;
}

public async Task<CommandResult<TOperationResult>> Command<TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult
{
_cancellationContextConstructor.Set(cancellationToken);
_cancellationContext.Set(cancellationToken);

var validator = _serviceProvider.GetService<IEasyWayValidator<TCommand>>();

Expand All @@ -41,7 +41,7 @@ public async Task<CommandResult<TOperationResult>> Command<TCommand, TOperationR
}
}

var commandHandler = _serviceProvider.GetRequiredService<ICommandHandler<TCommand,TOperationResult>>();
var commandHandler = _serviceProvider.GetRequiredService<CommandHandler<TCommand,TOperationResult>>();

var commandResult = await commandHandler.Handle(command);

Expand Down
10 changes: 2 additions & 8 deletions source/EasyWay/Internals/Commands/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ internal static IServiceCollection AddCommands(
services.AddScoped(typeof(ICommandExecutor<>).MakeGenericType(moduleType), typeof(CommandExecutor<>).MakeGenericType(moduleType));
services.AddScoped(typeof(ICommandWithOperationResultExecutor<>).MakeGenericType(moduleType), typeof(CommandWithOperationResultExecutor<>).MakeGenericType(moduleType));

services.Scan(s => s.FromAssemblies(assemblies)
.AddClasses(c => c.AssignableTo(typeof(ICommandHandler<>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
services.AddAsBasedType(typeof(CommandHandler<>), ServiceLifetime.Scoped, assemblies);

services.Scan(s => s.FromAssemblies(assemblies)
.AddClasses(c => c.AssignableTo(typeof(ICommandHandler<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
services.AddAsBasedType(typeof(CommandHandler<,>), ServiceLifetime.Scoped, assemblies);

services.AddScoped<IUnitOfWorkCommandHandler, UnitOfWorkCommandHandler>();

Expand Down
9 changes: 0 additions & 9 deletions source/EasyWay/Internals/Contexts/CancellationContext.cs

This file was deleted.

3 changes: 1 addition & 2 deletions source/EasyWay/Internals/Contexts/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ internal static class Extensions
{
internal static IServiceCollection AddContexts(this IServiceCollection services)
{
services.AddScoped<ICancellationContextConstructor, CancellationContext>();
services.AddScoped<ICancellationContext, CancellationContext>();
services.AddScoped<CancellationContext>();

services.AddScoped<IUserContext, DefaultUserContext>();
services.AddScoped<ICorrelationContext, DefaultCorrelationContext>();
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions source/EasyWay/Internals/DomainEvents/DomainEventPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public DomainEventPublisher(IServiceProvider serviceProvider)
public async Task Publish<TEvent>(TEvent @event)
where TEvent : DomainEvent
{
var handlerType = typeof(IDomainEventHandler<>).MakeGenericType(@event.GetType());
var handlerType = typeof(DomainEventHandler<>).MakeGenericType(@event.GetType());

var eventHandlers = _serviceProvider.GetServices(handlerType);

foreach (var eventHandler in eventHandlers)
{
await (Task)handlerType
.GetMethod(nameof(IDomainEventHandler<TEvent>.Handle))?
.GetMethod(nameof(DomainEventHandler<TEvent>.Handle))?
.Invoke(eventHandler, new object[] { @event });
}
}
Expand Down
5 changes: 1 addition & 4 deletions source/EasyWay/Internals/DomainEvents/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ internal static IServiceCollection AddDomainEvents(this IServiceCollection servi
services.AddScoped<IDomainEventBulkPublisher, DomainEventBulkPublisher>();
services.AddScoped<IDomainEventContextDispacher, DomainEventContextDispacher>();

services.Scan(s => s.FromAssemblies(assemblies)
.AddClasses(c => c.AssignableTo(typeof(IDomainEventHandler<>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
services.AddAsBasedType(typeof(DomainEventHandler<>), ServiceLifetime.Scoped, assemblies);

return services;
}
Expand Down
7 changes: 1 addition & 6 deletions source/EasyWay/Internals/DomainServices/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ namespace EasyWay.Internals.DomainServices
{
internal static class Extensions
{
private static Type _domainServiceType = typeof(DomainService);

internal static IServiceCollection AddDomainServices(
this IServiceCollection services,
IEnumerable<Assembly> assemblies)
{
services.Scan(s => s.FromAssemblies(assemblies)
.AddClasses(c => c.Where(x => x.IsSubclassOf(_domainServiceType)))
.AsSelf()
.WithTransientLifetime());
services.AddSelfOnBasedType(typeof(DomainService), ServiceLifetime.Transient, assemblies);

return services;
}
Expand Down
Loading
Loading