Skip to content

A simple library for implement 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.

License

H-Ghamarzadeh/HGO.Hub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HGO.Hub (.Net Library for implementing In-Process Messaging mechanism)

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.

NuGet version (HGO.Hub)

Installing HGO.Hub

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.

Registering with IServiceCollection

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 Singleton
  • IEventHandler<> as Scoped
  • IActionHandler<> as Scoped
  • IFilterHandler<> as Scoped
  • IRequestHandler<,> as Scoped

Messages Type

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 on Order 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 on Order 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 in Priority property). with these type of messages you can implement CQRS and Mediator pattern.

Publish an Event example:

  1. First add HGO.Hub service:
services.AddHgoHub(configuration =>
{
    configuration.HandlersDefaultLifetime = ServiceLifetime.Scoped;
    configuration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
});
  1. 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; }
}
  1. 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!");
    }
}
  1. Now you can publish the OnUserRegistered event when a user is registered in the system, you can publish the event via Hub class, the Hub class will identify all handlers for OnUserRegistered event and will sent the event for all of them in parallel and asynchronously :
await _hub.PublishEventAsync(new OnUserRegistered() { RegisteredUserId = userId });

More examples:

For more examples please check HGO.Hub.Test project, , I've written some unit tests for all type of messages!

If you have any kind of question or problem, I will be happy to contact me via email: h.ghamarzadeh@hotmail.com

About

A simple library for implement 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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages