From 35c65854701fff3bac4325595b83d50e3846fa9a Mon Sep 17 00:00:00 2001 From: Mirolim Majidov Date: Wed, 26 Feb 2025 16:58:53 +0500 Subject: [PATCH] Refactored the logic --- README.md | 16 ++++++------ src/Extensions/MemoryMessagingExtensions.cs | 6 ++--- ...MessagingManager.cs => IMessageManager.cs} | 2 +- ...yMessagingManager.cs => MessageManager.cs} | 2 +- src/Models/IMemoryMessaging.cs | 8 ------ src/Models/IMessage.cs | 2 +- .../Domain/UserCreated.cs | 6 ++--- .../Domain/UserUpdated.cs | 6 ++--- ...ManagerTests.cs => MessageManagerTests.cs} | 26 ++++++++++++------- .../Controllers/UserController.cs | 8 +++--- .../Messaging/Events/UserCreated.cs | 6 ++--- .../Messaging/Events/UserDeleted.cs | 6 ++--- .../Messaging/Events/UserUpdated.cs | 8 +++--- 13 files changed, 51 insertions(+), 51 deletions(-) rename src/Managers/{IMemoryMessagingManager.cs => IMessageManager.cs} (92%) rename src/Managers/{MemoryMessagingManager.cs => MessageManager.cs} (97%) delete mode 100644 src/Models/IMemoryMessaging.cs rename tests/InMemoryMessaging.Tests/UnitTests/{MemoryMessagingManagerTests.cs => MessageManagerTests.cs} (83%) diff --git a/README.md b/README.md index 6fa92f3..acf9ce3 100644 --- a/README.md +++ b/README.md @@ -76,14 +76,14 @@ builder.Services.AddInMemoryMessaging(assembliesToRegisterMessageHandlers); ### Create and publish an event massage -Start creating a message to publish. Your record must implement the `IMemoryMessaging` interface. Example: +Start creating a message to publish. Your record must implement the `IMessage` interface. Example: ``` -public record UserDeleted : IMemoryMessaging +public record UserDeleted : IMessage { - public Guid UserId { get; init; } + public required Guid UserId { get; init; } - public string UserName { get; init; } + public required string UserName { get; init; } } ``` @@ -107,16 +107,16 @@ Depend on your business logic, you need to add your logic to the `HandleAsync` m ### How to publish a message -To publish a message, you must first inject the `IMemoryMessagingManager` interface from the DI and pass your message object to the `PublishAsync` method. Then, your message will be published. +To publish a message, you must first inject the `IMessageManager` interface from the DI and pass your message object to the `PublishAsync` method. Then, your message will be published. ``` -public class UserController(IMemoryMessagingManager memoryMessagingManager) : ControllerBase +public class UserController(IMessageManager messageManager) : ControllerBase { [HttpPost] public async Task Create([FromBody] User item) { var userCreated = new UserCreated { UserId = item.Id, UserName = item.Name }; - await memoryMessagingManager.PublishAsync(userCreated); + await messageManager.PublishAsync(userCreated); return Ok(item); } @@ -127,5 +127,5 @@ public class UserController(IMemoryMessagingManager memoryMessagingManager) : Co Yes, we can. The library is designed to work with multiple a message handlers for the message type, even if there are multiple message types with the same name, we support them. So, when a message received, all handlers of a message will be executed. ### What Dependency Injection scope is used for the message handlers? -The library registers the `IMemoryMessagingManager` interface as a `Scoped` service. This means that all message handlers are created within the same scope as the request. It means that the scope of your service that is used to publish a message is the same as the scope of the message handler. +The library registers the `IMessageManager` interface as a `Scoped` service. This means that all message handlers are created within the same scope as the request. It means that the scope of your service that is used to publish a message is the same as the scope of the message handlers. diff --git a/src/Extensions/MemoryMessagingExtensions.cs b/src/Extensions/MemoryMessagingExtensions.cs index c958532..ece55fd 100644 --- a/src/Extensions/MemoryMessagingExtensions.cs +++ b/src/Extensions/MemoryMessagingExtensions.cs @@ -18,12 +18,12 @@ public static void AddInMemoryMessaging(this IServiceCollection services, Assembly[] assemblies, EventHandler executingReceivedMessage = null) { - services.AddScoped(); + services.AddScoped(); RegisterAllMessageHandlersToDependencyInjectionAndMessagingManager(services, assemblies); if (executingReceivedMessage is not null) - MemoryMessagingManager.ExecutingMessageHandlers += executingReceivedMessage; + MessageManager.ExecutingMessageHandlers += executingReceivedMessage; } #region Message Handlers Registration @@ -50,7 +50,7 @@ void RegisterAllSubscriberReceiversToDependencyInjection() void RegisterAllSubscriberReceiversToMemoryMessagingManager() { foreach (var (messageType, messageHandlerTypes) in allMessagesIncludingHandlers) - MemoryMessagingManager.AddHandlers(messageType, messageHandlerTypes.ToArray()); + MessageManager.AddHandlers(messageType, messageHandlerTypes.ToArray()); } } diff --git a/src/Managers/IMemoryMessagingManager.cs b/src/Managers/IMessageManager.cs similarity index 92% rename from src/Managers/IMemoryMessagingManager.cs rename to src/Managers/IMessageManager.cs index a2f2d00..506fad3 100644 --- a/src/Managers/IMemoryMessagingManager.cs +++ b/src/Managers/IMessageManager.cs @@ -5,7 +5,7 @@ namespace InMemoryMessaging.Managers; /// /// The interface to implement the memory messaging functionality. /// -public interface IMemoryMessagingManager +public interface IMessageManager { /// /// Publishing a message through the memory. diff --git a/src/Managers/MemoryMessagingManager.cs b/src/Managers/MessageManager.cs similarity index 97% rename from src/Managers/MemoryMessagingManager.cs rename to src/Managers/MessageManager.cs index 3a8bd1f..1984e28 100644 --- a/src/Managers/MemoryMessagingManager.cs +++ b/src/Managers/MessageManager.cs @@ -7,7 +7,7 @@ namespace InMemoryMessaging.Managers; -internal class MemoryMessagingManager(IServiceProvider serviceProvider) : IMemoryMessagingManager +internal class MessageManager(IServiceProvider serviceProvider) : IMessageManager { private static readonly Dictionary AllHandlers = new(); diff --git a/src/Models/IMemoryMessaging.cs b/src/Models/IMemoryMessaging.cs deleted file mode 100644 index bd34442..0000000 --- a/src/Models/IMemoryMessaging.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace InMemoryMessaging.Models; - -/// -/// An interface for finding all events that need to be communicat through memory. -/// -public interface IMemoryMessaging : IMessage -{ -} \ No newline at end of file diff --git a/src/Models/IMessage.cs b/src/Models/IMessage.cs index 5feee0b..3bf5cbf 100644 --- a/src/Models/IMessage.cs +++ b/src/Models/IMessage.cs @@ -1,7 +1,7 @@ namespace InMemoryMessaging.Models; /// -/// The base interface for all kind of messages/events. +/// An interface for finding all events that need to be communicat through memory. /// public interface IMessage { diff --git a/tests/InMemoryMessaging.Tests/Domain/UserCreated.cs b/tests/InMemoryMessaging.Tests/Domain/UserCreated.cs index 59dd779..8b071b6 100644 --- a/tests/InMemoryMessaging.Tests/Domain/UserCreated.cs +++ b/tests/InMemoryMessaging.Tests/Domain/UserCreated.cs @@ -1,11 +1,11 @@ using InMemoryMessaging.Models; namespace InMemoryMessaging.Tests.Domain; -public record UserCreated : IMemoryMessaging +public record UserCreated : IMessage { - public Guid Id { get; init; } + public required Guid Id { get; init; } - public string Name { get; init; } + public required string Name { get; init; } /// /// Fpr counting the number of times the message has been handled. diff --git a/tests/InMemoryMessaging.Tests/Domain/UserUpdated.cs b/tests/InMemoryMessaging.Tests/Domain/UserUpdated.cs index 10fbff9..9329a74 100644 --- a/tests/InMemoryMessaging.Tests/Domain/UserUpdated.cs +++ b/tests/InMemoryMessaging.Tests/Domain/UserUpdated.cs @@ -1,11 +1,11 @@ using InMemoryMessaging.Models; namespace InMemoryMessaging.Tests.Domain; -public record UserUpdated : IMemoryMessaging +public record UserUpdated : IMessage { - public Guid Id { get; init; } + public required Guid Id { get; init; } - public string Name { get; init; } + public required string Name { get; init; } /// /// Fpr counting the number of times the event has been handled. diff --git a/tests/InMemoryMessaging.Tests/UnitTests/MemoryMessagingManagerTests.cs b/tests/InMemoryMessaging.Tests/UnitTests/MessageManagerTests.cs similarity index 83% rename from tests/InMemoryMessaging.Tests/UnitTests/MemoryMessagingManagerTests.cs rename to tests/InMemoryMessaging.Tests/UnitTests/MessageManagerTests.cs index fa73cde..c2c1126 100644 --- a/tests/InMemoryMessaging.Tests/UnitTests/MemoryMessagingManagerTests.cs +++ b/tests/InMemoryMessaging.Tests/UnitTests/MessageManagerTests.cs @@ -8,16 +8,16 @@ namespace InMemoryMessaging.Tests.UnitTests; -public class MemoryMessagingManagerTests : BaseTestEntity +public class MessageManagerTests : BaseTestEntity { private readonly ServiceProvider _serviceProvider; #region SutUp - public MemoryMessagingManagerTests() + public MessageManagerTests() { ServiceCollection serviceCollection = new(); - Assembly[] assemblies = [typeof(MemoryMessagingManagerTests).Assembly]; + Assembly[] assemblies = [typeof(MessageManagerTests).Assembly]; MemoryMessagingExtensions.RegisterAllMessageHandlersToDependencyInjectionAndMessagingManager (serviceCollection, assemblies); @@ -57,7 +57,7 @@ public void AddHandlers_RegisteringMessageTypeWithHandlersTwice_MessageHandlersI var messageType = typeof(UserCreated); var messageHandlerType1 = typeof(Domain.Module1.UserCreatedHandler); var messageHandlerType2 = typeof(Domain.Module2.UserCreatedHandler); - MemoryMessagingManager.AddHandlers(messageType, [messageHandlerType1, messageHandlerType2]); + MessageManager.AddHandlers(messageType, [messageHandlerType1, messageHandlerType2]); var handlersInfo = GetAllHandlersInfo(); Assert.That(handlersInfo.ContainsKey(messageType.Name), Is.True); @@ -74,8 +74,12 @@ public void AddHandlers_RegisteringMessageTypeWithHandlersTwice_MessageHandlersI public async Task PublishAsync_PublishingMessageWhichDoesNotHaveHandler_ShouldNotBeExecuted() { - var memoryMessagingManager = new MemoryMessagingManager(_serviceProvider); - var message = new UserUpdated(); + var memoryMessagingManager = new MessageManager(_serviceProvider); + var message = new UserUpdated + { + Id = Guid.NewGuid(), + Name = "User Name" + }; await memoryMessagingManager.PublishAsync(message); @@ -86,8 +90,12 @@ public async Task public async Task PublishAsync_PublishingMessageWhichHasTwoHandlers_ShouldBeExecutedTwice() { - var memoryMessagingManager = new MemoryMessagingManager(_serviceProvider); - var message = new UserCreated(); + var memoryMessagingManager = new MessageManager(_serviceProvider); + var message = new UserCreated + { + Id = Guid.NewGuid(), + Name = "User Name" + }; await memoryMessagingManager.PublishAsync(message); @@ -114,7 +122,7 @@ public void OneTimeTearDown() private Dictionary GetAllHandlersInfo() { const string handlersFieldName = "AllHandlers"; - var field = typeof(MemoryMessagingManager).GetField(handlersFieldName, + var field = typeof(MessageManager).GetField(handlersFieldName, BindingFlags.NonPublic | BindingFlags.Static); Assert.That(handlersFieldName, Is.Not.Null); diff --git a/tests/Services/UsersService/Controllers/UserController.cs b/tests/Services/UsersService/Controllers/UserController.cs index 475c85b..3e1a366 100644 --- a/tests/Services/UsersService/Controllers/UserController.cs +++ b/tests/Services/UsersService/Controllers/UserController.cs @@ -7,7 +7,7 @@ namespace UsersService.Controllers; [ApiController] [Route("[controller]")] -public class UserController(IMemoryMessagingManager memoryMessagingManager) : ControllerBase +public class UserController(IMessageManager messageManager) : ControllerBase { private static readonly Dictionary Items = new(); @@ -32,7 +32,7 @@ public async Task Create([FromBody] User item) Items.Add(item.Id, item); var userCreated = new UserCreated { UserId = item.Id, UserName = item.Name }; - await memoryMessagingManager.PublishAsync(userCreated); + await messageManager.PublishAsync(userCreated); return Ok(); } @@ -46,7 +46,7 @@ public async Task Update(Guid id, [FromQuery] string newName) var userUpdated = new UserUpdated { UserId = item.Id, OldUserName = item.Name, NewUserName = newName }; item.Name = newName; - await memoryMessagingManager.PublishAsync(userUpdated); + await messageManager.PublishAsync(userUpdated); return Ok(item); } @@ -59,7 +59,7 @@ public async Task Delete(Guid id) var userDeleted = new UserDeleted { UserId = item.Id, UserName = item.Name }; Items.Remove(id); - await memoryMessagingManager.PublishAsync(userDeleted); + await messageManager.PublishAsync(userDeleted); return Ok(item); } diff --git a/tests/Services/UsersService/Messaging/Events/UserCreated.cs b/tests/Services/UsersService/Messaging/Events/UserCreated.cs index 83c5578..b21c513 100644 --- a/tests/Services/UsersService/Messaging/Events/UserCreated.cs +++ b/tests/Services/UsersService/Messaging/Events/UserCreated.cs @@ -2,11 +2,11 @@ namespace UsersService.Messaging.Events; -public record UserCreated : IMemoryMessaging +public record UserCreated : IMessage { - public Guid UserId { get; init; } + public required Guid UserId { get; init; } - public string UserName { get; init; } + public required string UserName { get; init; } /// /// Fpr counting the number of times the message has been handled. diff --git a/tests/Services/UsersService/Messaging/Events/UserDeleted.cs b/tests/Services/UsersService/Messaging/Events/UserDeleted.cs index 74476a5..cc26f5f 100644 --- a/tests/Services/UsersService/Messaging/Events/UserDeleted.cs +++ b/tests/Services/UsersService/Messaging/Events/UserDeleted.cs @@ -2,9 +2,9 @@ namespace UsersService.Messaging.Events; -public record UserDeleted : IMemoryMessaging +public record UserDeleted : IMessage { - public Guid UserId { get; init; } + public required Guid UserId { get; init; } - public string UserName { get; init; } + public required string UserName { get; init; } } \ No newline at end of file diff --git a/tests/Services/UsersService/Messaging/Events/UserUpdated.cs b/tests/Services/UsersService/Messaging/Events/UserUpdated.cs index 0a2c6f1..14959f1 100644 --- a/tests/Services/UsersService/Messaging/Events/UserUpdated.cs +++ b/tests/Services/UsersService/Messaging/Events/UserUpdated.cs @@ -2,11 +2,11 @@ namespace UsersService.Messaging.Events; -public record UserUpdated : IMemoryMessaging +public record UserUpdated : IMessage { - public Guid UserId { get; init; } + public required Guid UserId { get; init; } - public string OldUserName { get; init; } + public required string OldUserName { get; init; } - public string NewUserName { get; init; } + public required string NewUserName { get; init; } } \ No newline at end of file