Skip to content

Commit 8d6e9e3

Browse files
authored
Changed handlers from interface to class and changed dependency injection (#77)
* Moved from interfaces to abstract class for handlers and changed depejdency injection * Changed CancellationContext from interfaces to sealed class * Refactored code
1 parent 3b0f73e commit 8d6e9e3

28 files changed

Lines changed: 183 additions & 106 deletions

samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace EasyWay.Samples.Commands
44
{
5-
public class ErrorCommandHandler : ICommandHandler<ErrorCommand>
5+
public sealed class ErrorCommandHandler : CommandHandler<ErrorCommand>
66
{
7-
public Task<CommandResult> Handle(ErrorCommand command)
7+
public sealed override Task<CommandResult> Handle(ErrorCommand command)
88
{
99
var x = new SampleAggregateRoot();
1010

samples/EasyWay.Samples/Commands/SampleCommandHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace EasyWay.Samples.Commands
55
{
6-
internal sealed class SampleCommandHandler : ICommandHandler<SampleCommand>
6+
internal sealed class SampleCommandHandler : CommandHandler<SampleCommand>
77
{
8-
private readonly ICancellationContext _cancellationContext;
8+
private readonly CancellationContext _cancellationContext;
99

1010
private readonly ISampleAggragateRootRepository _repository;
1111

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

2222
public SampleCommandHandler(
23-
ICancellationContext cancellationContext,
23+
CancellationContext cancellationContext,
2424
ISampleAggragateRootRepository repository,
2525
SampleAggregateRootFactory factory,
2626
SampleDomainService domainService,
@@ -37,7 +37,7 @@ public SampleCommandHandler(
3737
_userContext = userContext;
3838
}
3939

40-
public async Task<CommandResult> Handle(SampleCommand command)
40+
public sealed override async Task<CommandResult> Handle(SampleCommand command)
4141
{
4242
var userId = _userContext.UserId;
4343

samples/EasyWay.Samples/Commands/WithResult/SampleCommandWithResultHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
namespace EasyWay.Samples.Commands.WithResult
33
{
4-
public sealed class SampleCommandWithResultHandler : ICommandHandler<SampleCommandWithResult, SampleCommandResult>
4+
public sealed class SampleCommandWithResultHandler : CommandHandler<SampleCommandWithResult, SampleCommandResult>
55
{
6-
public Task<CommandResult<SampleCommandResult>> Handle(SampleCommandWithResult command)
6+
public sealed override Task<CommandResult<SampleCommandResult>> Handle(SampleCommandWithResult command)
77
{
88
return Task.FromResult(CommandResult<SampleCommandResult>.Ok(new SampleCommandResult()));
99
}

samples/EasyWay.Samples/Events/SampleEventHandler1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace EasyWay.Samples.Events
44
{
5-
public class SampleEventHandler1 : IDomainEventHandler<CreatedSampleAggragete>
5+
public sealed class SampleEventHandler1 : DomainEventHandler<CreatedSampleAggragete>
66
{
7-
public Task Handle(CreatedSampleAggragete @event)
7+
public sealed override Task Handle(CreatedSampleAggragete domainEvent)
88
{
99
return Task.CompletedTask;
1010
}

samples/EasyWay.Samples/Events/SampleEventHandler2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace EasyWay.Samples.Events
44
{
5-
public class SampleEventHandler2 : IDomainEventHandler<CreatedSampleAggragete>
5+
public sealed class SampleEventHandler2 : DomainEventHandler<CreatedSampleAggragete>
66
{
7-
public Task Handle(CreatedSampleAggragete @event)
7+
public sealed override Task Handle(CreatedSampleAggragete domainEvent)
88
{
99
return Task.CompletedTask;
1010
}

samples/EasyWay.Samples/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using EasyWay.Samples;
22
using EasyWay.Samples.Commands;
33
using EasyWay.Samples.Commands.WithResult;
4+
using EasyWay.Samples.Domain.Policies;
45
using EasyWay.Samples.Queries;
56
using Microsoft.AspNetCore.Mvc;
67

@@ -40,7 +41,9 @@ await kernel
4041

4142
app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
4243
{
43-
return await executor.Command(command);
44+
var x = await executor.Command(command);
45+
46+
return mapper.Map(x);
4447
});
4548

4649
app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>

samples/EasyWay.Samples/Queries/SampleQueryHandler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
namespace EasyWay.Samples.Queries
22
{
3-
public class SampleQueryHandler : IQueryHandler<SampleQuery, SampleQueryResult>
3+
internal sealed class SampleQueryHandler : QueryHandler<SampleQuery, SampleQueryResult>
44
{
5-
public Task<QueryResult<SampleQueryResult>> Handle(SampleQuery query)
5+
public sealed override Task<QueryResult<SampleQueryResult>> Handle(SampleQuery query)
66
{
7-
//return Task.FromResult(QueryResult<SampleQueryResult>.Forbidden);
8-
97
return Task.FromResult(QueryResult<SampleQueryResult>.Ok(new SampleQueryResult()));
108
}
119
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace EasyWay
2+
{
3+
public sealed class CancellationContext
4+
{
5+
public CancellationToken Token { get; private set; }
6+
7+
internal void Set(CancellationToken token) => Token = token;
8+
}
9+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
/// Defines a handler for a command
55
/// </summary>
66
/// <typeparam name="TCommand">The type of command being handled</typeparam>
7-
public interface ICommandHandler<TCommand>
7+
public abstract class CommandHandler<TCommand>
88
where TCommand : Command
99
{
1010
/// <summary>
1111
/// Handles a command
1212
/// </summary>
1313
/// <param name="command">Command</param>
14-
Task<CommandResult> Handle(TCommand command);
14+
public abstract Task<CommandResult> Handle(TCommand command);
1515
}
1616

17-
public interface ICommandHandler<TCommand, TOperationResult>
17+
public abstract class CommandHandler<TCommand, TOperationResult>
1818
where TCommand : Command<TOperationResult>
1919
where TOperationResult : OperationResult
2020
{
21-
Task<CommandResult<TOperationResult>> Handle(TCommand command);
21+
public abstract Task<CommandResult<TOperationResult>> Handle(TCommand command);
2222
}
2323
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
/// Defines a handler for an event
55
/// </summary>
66
/// <typeparam name="TDomainEvent">The type of event being handled</typeparam>
7-
public interface IDomainEventHandler<TDomainEvent>
7+
public abstract class DomainEventHandler<TDomainEvent>
88
where TDomainEvent : DomainEvent
99
{
1010
/// <summary>
1111
/// Handles an event
1212
/// </summary>
1313
/// <param name="domainEvent">Event</param>
14-
Task Handle(TDomainEvent domainEvent);
14+
public abstract Task Handle(TDomainEvent domainEvent);
1515
}
1616
}

0 commit comments

Comments
 (0)