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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
```

Expand All @@ -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<IActionResult> 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);
}
Expand All @@ -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.

6 changes: 3 additions & 3 deletions src/Extensions/MemoryMessagingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public static void AddInMemoryMessaging(this IServiceCollection services,
Assembly[] assemblies,
EventHandler<ReceivedMessageArgs> executingReceivedMessage = null)
{
services.AddScoped<IMemoryMessagingManager, MemoryMessagingManager>();
services.AddScoped<IMessageManager, MessageManager>();

RegisterAllMessageHandlersToDependencyInjectionAndMessagingManager(services, assemblies);

if (executingReceivedMessage is not null)
MemoryMessagingManager.ExecutingMessageHandlers += executingReceivedMessage;
MessageManager.ExecutingMessageHandlers += executingReceivedMessage;
}

#region Message Handlers Registration
Expand All @@ -50,7 +50,7 @@ void RegisterAllSubscriberReceiversToDependencyInjection()
void RegisterAllSubscriberReceiversToMemoryMessagingManager()
{
foreach (var (messageType, messageHandlerTypes) in allMessagesIncludingHandlers)
MemoryMessagingManager.AddHandlers(messageType, messageHandlerTypes.ToArray());
MessageManager.AddHandlers(messageType, messageHandlerTypes.ToArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace InMemoryMessaging.Managers;
/// <summary>
/// The interface to implement the memory messaging functionality.
/// </summary>
public interface IMemoryMessagingManager
public interface IMessageManager
{
/// <summary>
/// Publishing a message through the memory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace InMemoryMessaging.Managers;

internal class MemoryMessagingManager(IServiceProvider serviceProvider) : IMemoryMessagingManager
internal class MessageManager(IServiceProvider serviceProvider) : IMessageManager
{
private static readonly Dictionary<string, MessageHandlerInformation[]> AllHandlers = new();

Expand Down
8 changes: 0 additions & 8 deletions src/Models/IMemoryMessaging.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Models/IMessage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace InMemoryMessaging.Models;

/// <summary>
/// The base interface for all kind of messages/events.
/// An interface for finding all events that need to be communicat through memory.
/// </summary>
public interface IMessage
{
Expand Down
6 changes: 3 additions & 3 deletions tests/InMemoryMessaging.Tests/Domain/UserCreated.cs
Original file line number Diff line number Diff line change
@@ -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; }

/// <summary>
/// Fpr counting the number of times the message has been handled.
Expand Down
6 changes: 3 additions & 3 deletions tests/InMemoryMessaging.Tests/Domain/UserUpdated.cs
Original file line number Diff line number Diff line change
@@ -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; }

/// <summary>
/// Fpr counting the number of times the event has been handled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -114,7 +122,7 @@ public void OneTimeTearDown()
private Dictionary<string, MessageHandlerInformation[]> 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);

Expand Down
8 changes: 4 additions & 4 deletions tests/Services/UsersService/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Guid, User> Items = new();

Expand All @@ -32,7 +32,7 @@ public async Task<IActionResult> 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();
}
Expand All @@ -46,7 +46,7 @@ public async Task<IActionResult> 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);
}
Expand All @@ -59,7 +59,7 @@ public async Task<IActionResult> 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);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Services/UsersService/Messaging/Events/UserCreated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

/// <summary>
/// Fpr counting the number of times the message has been handled.
Expand Down
6 changes: 3 additions & 3 deletions tests/Services/UsersService/Messaging/Events/UserDeleted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
8 changes: 4 additions & 4 deletions tests/Services/UsersService/Messaging/Events/UserUpdated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}