-
Notifications
You must be signed in to change notification settings - Fork 0
NetHacksPack.Integration.Abstractions
This package contains the abstractions from providers to help build integrations with different applications using a EventBus channnel interface.
The applications will use the provided interfaces and the implementations should be configured on the Dependency Injection Container. This allows to design applications that can easily change their infrastructure to others kind of channels without causes significant break changes to the project.
To use it in your project you can dowload diretly from Nuget:
dotnet add NetHacksPack.Integration.AbstractionsThe main interface in this package is IEventBus, this interface provides the mechanism to use an event bus, since connect an listen an event, till to publish one. The purpose of this interface is to be the abstraction used in any part on the project as the only way to handle the based event communications.
In general we use to develop by demand, and sometimes we create some coupling with the current infrastruct violating some SOLID principles. To avoid this kind of coupling with event bus the developers can use this package to publish their events or subscribe to them.
This Package contains some dependencies to work, each event should inherit from NetHacksPack.Core.Event, an abstract class that defines what is an Event.
When you subscribe to an event you need to ensure that you have a registered handler on the eventbus and also on dependency injection container.
A handler is a class that can handle the message received by the bus and should implements the interface Nethacks.Core.IEventHandler<>, where TEventDesc should inherit from NetHacksPack.Core.Event class.
Bellow an example of an EventHandler implementation:
using NetHacksPack.Core;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
namespace MySystem
{
[DisplayName("payment-approved")]
public class PaymentApprovedEvent : Event
{
public PaymentApprovedEvent() : base("payment-approved")
{
}
public decimal Value { get; set; }
public Guid PaymentId { get; set; }
public Guid OrderId { get; set; }
}
public class PaymentApprovedHandle : IEventHandler<PaymentApprovedEvent>
{
private readonly IMyService service;
public PaymentApproved(IMyService service)
{
this.service = service;
}
public async Task Handle(PaymentApprovedEvent @event)
{
await this.service.ReleaseOrder(@event.OrderId);
}
}
}After that we need to subscribe into the event
public class Startup
{
public void ConfigureServices(IServiceProvider services)
{
services.AddScoped<IEventHandler<PaymentApprovedEvent>, PaymentApprovedHandle>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IEventBus @eventBus)
{
@eventBus.Subscribe<PaymentApprovedEvent, PaymentApprovedHandle>();
// or you can also register with the interface
@eventBus.Subscribe<PaymentApprovedEvent, IEventHandler<PaymentApprovedEvent>>();
// but with the interface is required to register it on the dependency injection container
}
}Now to send an event you can just publish them with the eventbus
public class PaymentGatwayApiController : Controller
{
private readonly IEventBus eventBus;
public MyController(IEventBus eventBus)
{
this.eventBus = eventBus;
}
public async Task<IActionResult> Post([FromBody] CreditCardMessage message)
{
await this.eventBus.PublishAsync(new PaymentApprovedEvent(message.Value, message.OrderId);
return Ok();
}
}