Skip to content

NetHacksPack.Integration.Abstractions

Edney Batista da Silva edited this page Jul 2, 2021 · 10 revisions

Get Started

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.

Usage

To use it in your project you can dowload diretly from Nuget:

dotnet add NetHacksPack.Integration.Abstractions

The 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.

Dependencies

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;
   }
   
   [HttpPost]
   public async Task<IActionResult> Post([FromBody] CreditCardMessage message)
   {
      await this.eventBus.PublishAsync(new PaymentApprovedEvent(message.Value, message.OrderId);
      return Ok();
   }
}

Provided Interfaces

Besides the interface IEventBus described above, this package provides a set of interfaces that should be used to implement the custom eventbus infrastructure.

IConnectionProvider - this inferface should be used to implement the connection option provider, as for exemple, custom options, connection strings.

public void ConnectToSomewhere(IConnectionProvider<MyConnectionOptions> options)
{
   var option = options.GetConnection("myconfigurations");
   var server = option.ServerName;
   var user = option.User;
   var password = option.Password;
   // do something with the options
}

IMessageReader - this interface should be used to implement the message reader, so here you can use to handle a json, a serialized byte, a splited messages or wherelse you want.

public MyMessage ReadMessage(string json)
{
   IMessageReader reader = new MyCustomJsonMessageReader(json);
   var myMessage = reader.Read<MyMessage>();
   return myMessage;
}

IMessageWriter - this interface should be used to implement the message writer, so here you can use to handle how to serialize a message as a json, byte array, or a splited message.

public string WriteMessage(MyMessage message, IMessageChannel channel)
{
   IMessageWriter writer = newq MyCustomJsonMessageWriter();
   string json = writer.Write(channel, message);
   return json;
}

The method responsible to serialize the message contains a generic parameter called TPublishChannel, use it to pass an object that can handle custom configurations to the channel

IEventBusErrorHandle - this interface should be used to implement handler to the errors occurred into the EventBus

public class GenericEventBusErrorHandlers : IEventBusErrorHandle
{
   public Task Handle(EventBusError busError)
   {
      Console.WriteLine(busErrors.Details);
      return Task.Complete;
   }
}

IEventBusListener - this interface should be used to implement the infrastructur to listen and receive the messages from the bus.

public class MyEventBus
{
   private readonly IEventBusListener eventListener;
   public void ConnectToTheBus()
   {
      eventListener.StartListener(CancellationToken.None);
   }
}

IEventBusManager - this interface should be used to implement the main managments to the eventbus infrastructure, in the current version only the errors are being managed

Clone this wiki locally