Working on a command dispatcher
This commit is contained in:
parent
31666b4ba0
commit
a86a2d7ade
5 changed files with 55 additions and 4 deletions
13
PlanTempus.Components/CommandHandler.cs
Normal file
13
PlanTempus.Components/CommandHandler.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
using Autofac;
|
||||||
|
using PlanTempus.Components.Users.Create;
|
||||||
|
|
||||||
|
namespace PlanTempus.Components;
|
||||||
|
|
||||||
|
public class CommandHandler(IComponentContext context) : ICommandHandler
|
||||||
|
{
|
||||||
|
public Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command)
|
||||||
|
{
|
||||||
|
var handler = context.Resolve<ICommandHandler<TCommand, TCommandResult>>();
|
||||||
|
return handler.Handle(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ namespace PlanTempus.Components.ModuleRegistry
|
||||||
|
|
||||||
// Registrer en decorator for alle ICommandHandler<TCommand>
|
// Registrer en decorator for alle ICommandHandler<TCommand>
|
||||||
builder.RegisterGenericDecorator(
|
builder.RegisterGenericDecorator(
|
||||||
typeof(LoggingCommandHandlerDecorator<>), // Din decorator-klasse
|
typeof(CreateUserHandlerDecorator<>), // Din decorator-klasse
|
||||||
typeof(ICommandHandler<>)); // Interface, der skal dekoreres
|
typeof(ICommandHandler<>)); // Interface, der skal dekoreres
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -7,8 +7,8 @@ namespace PlanTempus.Components.Users.Create
|
||||||
|
|
||||||
public class CreateUserCommand : ICommand
|
public class CreateUserCommand : ICommand
|
||||||
{
|
{
|
||||||
public string Email { get; set; }
|
public required string Email { get; set; }
|
||||||
public string Password { get; set; }
|
public required string Password { get; set; }
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
public required Guid CorrelationId { get; set; }
|
public required Guid CorrelationId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,12 @@ using PlanTempus.Core.Telemetry;
|
||||||
|
|
||||||
namespace PlanTempus.Components.Users.Create
|
namespace PlanTempus.Components.Users.Create
|
||||||
{
|
{
|
||||||
public interface ICommandHandler<T, TResult>
|
public interface ICommandHandler
|
||||||
|
{
|
||||||
|
Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command );
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ICommandHandler<in T, TResult>
|
||||||
{
|
{
|
||||||
Task<TResult> Handle(T input);
|
Task<TResult> Handle(T input);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
33
Tests/CommandQueryHandlerTests/HandlerTest.cs
Normal file
33
Tests/CommandQueryHandlerTests/HandlerTest.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
using Autofac;
|
||||||
|
using Insight.Database;
|
||||||
|
using PlanTempus.Components;
|
||||||
|
using PlanTempus.Components.Users.Create;
|
||||||
|
using PlanTempus.Core.Database;
|
||||||
|
using PlanTempus.Core.Database.ConnectionFactory;
|
||||||
|
|
||||||
|
namespace PlanTempus.X.TDD.CommandQueryHandlerTests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class HandlerTest : TestFixture
|
||||||
|
{
|
||||||
|
[TestInitialize]
|
||||||
|
public void This()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestDefaultConnection()
|
||||||
|
{
|
||||||
|
var commandHandler = Container.Resolve<CommandHandler>();
|
||||||
|
|
||||||
|
var command = new CreateUserCommand
|
||||||
|
{
|
||||||
|
Email = "lloyd@dumbanddumber.com", // Lloyd Christmas
|
||||||
|
Password = "1234AceVentura#LOL", // Ace Ventura
|
||||||
|
IsActive = true,
|
||||||
|
CorrelationId = Guid.NewGuid()
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = commandHandler.Handle<CreateUserCommand, CreateUserResult>(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue