A simple library for implementing Event Sourcing, Pub/Sub, Mediator, CQRS Pattern with multiple handlers in .NET. With this package you can easily implement Wordpress Hooks (Action/Filter) in your ASP.Net project.
You should install HGO.Hub with NuGet:
Install-Package HGO.Hub
Or via the .NET Core command line interface:
dotnet add package HGO.Hub
Either commands, from Package Manager Console or .NET Core CLI, will download and install HGO.Hub and all required dependencies.
HGO.Hub supports Microsoft.Extensions.DependencyInjection.Abstractions
directly. To register various HGO.Hub services and handlers:
services.AddHgoHub(configuration =>
{
configuration.HandlersDefaultLifetime = ServiceLifetime.Scoped;
configuration.RegisterServicesFromAssemblyContaining<Startup>();
});
or with an assembly:
services.AddHgoHub(configuration =>
{
configuration.HandlersDefaultLifetime = ServiceLifetime.Scoped;
configuration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
});
This registers:
IHub
as SingletonIEventHandler<>
as ScopedIActionHandler<>
as ScopedIFilterHandler<>
as ScopedIRequestHandler<,>
as Scoped
There are 4 type of messages that you can send via IHub
into pipeline, each type of message will be delivered automatically to corresponding handler.
Event
: These type of messages will be published into pipeline as an event, events will be handled by multiple corresponding handlers asynchronously and in parallel. Also, events do not return any value.Action
: These type of messages will be published into pipeline as an action, actions will be handled by multiple corresponding handlers asynchronously and sequentially (based onOrder
property - lower numbers correspond with earlier execution). These types of messages are equal to events, with the difference that they are executed sequentially. Also, actions do not return any value. In general, actions are similar to WordPress Actions.Filter
: Asynchronously and sequentially (based onOrder
property - lower numbers correspond with earlier execution) applies all the corresponding filter handlers which registered in the pipeline to the data and returns the filtered data. It has the same functionality as WordPress filters.Request
: Asynchronously will sent a request (Command/Query) to corresponding handler and will return the response. You can have multiple handlers for a request, but just one of them will be executed (which has larger number inPriority
property). with these type of messages you can implement CQRS and Mediator pattern.
- First add HGO.Hub service:
services.AddHgoHub(configuration =>
{
configuration.HandlersDefaultLifetime = ServiceLifetime.Scoped;
configuration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
});
- Define a class which inherits from IEvent interface and add your desired properties which contain information about event:
public class OnUserRegistered: IEvent
{
public int RegisteredUserId { get; set; }
}
- Now you must define a handler for this event:
public class SendEmailOnUserRegisteredEventHandler(IHub hub, IEmailService emailService) : IEventHandler<OnUserRegistered>
{
public async Task Handle(OnUserRegistered @event)
{
var user = await hub.RequestAsync(new GetUserRequest(@event.RegisteredUserId));
await emailService.SendEmail(user.EmailAddress, "Hello", $"Dear {user.FirstName} {user.LastName}, Thank you for join us!");
}
}
- Now you can publish the
OnUserRegistered
event when a user is registered in the system, you can publish the event viaHub
class, theHub
class will identify all handlers forOnUserRegistered
event and will sent the event for all of them in parallel and asynchronously :
await _hub.PublishEventAsync(new OnUserRegistered() { RegisteredUserId = userId });
For more examples please check HGO.Hub.Test
project, , I've written some unit tests for all type of messages!