Giter Site home page Giter Site logo

h-ghamarzadeh / hgo.hub Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 87 KB

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: MIT License

C# 100.00%
asp-net-core asp-net-core-mvc cqrs-framework cqrs-pattern event-sourcing mediator-pattern

hgo.hub's Introduction

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: [email protected]

hgo.hub's People

Contributors

h-ghamarzadeh avatar

Stargazers

Hoang Manh Phu avatar ashmah24 avatar  avatar  avatar Mehmet avatar Vahid Saberi avatar  avatar

Watchers

Hoang Manh Phu avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.