From a86a2d7aded408b9c6ceeeff4cc1dbd801f0a7ce Mon Sep 17 00:00:00 2001 From: Janus Knudsen Date: Mon, 10 Mar 2025 18:10:00 +0100 Subject: [PATCH] Working on a command dispatcher --- PlanTempus.Components/CommandHandler.cs | 13 ++++++++ .../{SeqLoggingModule.cs => CommandModule.cs} | 2 +- .../Users/Create/CreateUserCommand.cs | 4 +-- .../Users/Create/CreateUserHandler.cs | 7 +++- Tests/CommandQueryHandlerTests/HandlerTest.cs | 33 +++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 PlanTempus.Components/CommandHandler.cs rename PlanTempus.Components/ModuleRegistry/{SeqLoggingModule.cs => CommandModule.cs} (89%) create mode 100644 Tests/CommandQueryHandlerTests/HandlerTest.cs diff --git a/PlanTempus.Components/CommandHandler.cs b/PlanTempus.Components/CommandHandler.cs new file mode 100644 index 0000000..36d534d --- /dev/null +++ b/PlanTempus.Components/CommandHandler.cs @@ -0,0 +1,13 @@ +using Autofac; +using PlanTempus.Components.Users.Create; + +namespace PlanTempus.Components; + +public class CommandHandler(IComponentContext context) : ICommandHandler +{ + public Task Handle(TCommand command) + { + var handler = context.Resolve>(); + return handler.Handle(command); + } +} \ No newline at end of file diff --git a/PlanTempus.Components/ModuleRegistry/SeqLoggingModule.cs b/PlanTempus.Components/ModuleRegistry/CommandModule.cs similarity index 89% rename from PlanTempus.Components/ModuleRegistry/SeqLoggingModule.cs rename to PlanTempus.Components/ModuleRegistry/CommandModule.cs index 559a8fc..ff01a3a 100644 --- a/PlanTempus.Components/ModuleRegistry/SeqLoggingModule.cs +++ b/PlanTempus.Components/ModuleRegistry/CommandModule.cs @@ -16,7 +16,7 @@ namespace PlanTempus.Components.ModuleRegistry // Registrer en decorator for alle ICommandHandler builder.RegisterGenericDecorator( - typeof(LoggingCommandHandlerDecorator<>), // Din decorator-klasse + typeof(CreateUserHandlerDecorator<>), // Din decorator-klasse typeof(ICommandHandler<>)); // Interface, der skal dekoreres } diff --git a/PlanTempus.Components/Users/Create/CreateUserCommand.cs b/PlanTempus.Components/Users/Create/CreateUserCommand.cs index 32bbe58..7a69f53 100644 --- a/PlanTempus.Components/Users/Create/CreateUserCommand.cs +++ b/PlanTempus.Components/Users/Create/CreateUserCommand.cs @@ -7,8 +7,8 @@ namespace PlanTempus.Components.Users.Create public class CreateUserCommand : ICommand { - public string Email { get; set; } - public string Password { get; set; } + public required string Email { get; set; } + public required string Password { get; set; } public bool IsActive { get; set; } = true; public required Guid CorrelationId { get; set; } } diff --git a/PlanTempus.Components/Users/Create/CreateUserHandler.cs b/PlanTempus.Components/Users/Create/CreateUserHandler.cs index 8c72b69..af96227 100644 --- a/PlanTempus.Components/Users/Create/CreateUserHandler.cs +++ b/PlanTempus.Components/Users/Create/CreateUserHandler.cs @@ -8,7 +8,12 @@ using PlanTempus.Core.Telemetry; namespace PlanTempus.Components.Users.Create { - public interface ICommandHandler + public interface ICommandHandler + { + Task Handle(TCommand command ); + } + + public interface ICommandHandler { Task Handle(T input); } diff --git a/Tests/CommandQueryHandlerTests/HandlerTest.cs b/Tests/CommandQueryHandlerTests/HandlerTest.cs new file mode 100644 index 0000000..dd77a0c --- /dev/null +++ b/Tests/CommandQueryHandlerTests/HandlerTest.cs @@ -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(); + + var command = new CreateUserCommand + { + Email = "lloyd@dumbanddumber.com", // Lloyd Christmas + Password = "1234AceVentura#LOL", // Ace Ventura + IsActive = true, + CorrelationId = Guid.NewGuid() + }; + + var result = commandHandler.Handle(command); + } +} \ No newline at end of file