HGO.Hub
1.0.3
See the version list below for details.
dotnet add package HGO.Hub --version 1.0.3
NuGet\Install-Package HGO.Hub -Version 1.0.3
<PackageReference Include="HGO.Hub" Version="1.0.3" />
paket add HGO.Hub --version 1.0.3
#r "nuget: HGO.Hub, 1.0.3"
// Install HGO.Hub as a Cake Addin #addin nuget:?package=HGO.Hub&version=1.0.3 // Install HGO.Hub as a Cake Tool #tool nuget:?package=HGO.Hub&version=1.0.3
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.
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 SingletonIEventHandler<>
as ScopedIActionHandler<>
as ScopedIFilterHandler<>
as ScopedIRequestHandler<,>
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 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.
Publish an Event example:
- 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 });
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 | Versions 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. |
-
.NETStandard 2.0
-
net6.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.