HGO.Hub 1.0.8

dotnet add package HGO.Hub --version 1.0.8                
NuGet\Install-Package HGO.Hub -Version 1.0.8                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HGO.Hub" Version="1.0.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HGO.Hub --version 1.0.8                
#r "nuget: HGO.Hub, 1.0.8"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HGO.Hub as a Cake Addin
#addin nuget:?package=HGO.Hub&version=1.0.8

// Install HGO.Hub as a Cake Tool
#tool nuget:?package=HGO.Hub&version=1.0.8                

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
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 80 5/2/2024
1.0.7 71 5/2/2024
1.0.6 124 4/30/2024
1.0.5 119 4/20/2024
1.0.4 139 3/7/2024
1.0.3 120 3/5/2024