Skip to content

ERNI-Academy/assets-events-abstraction

Repository files navigation

About Events Abstraction

Define a contract and many implementations (Azure EventGrid, Azure ServiceBus, Azure Storage Queues, Azure Redis) for publish and subscribe

ERNI Academy StarterKit, PoC, or Gidelines. This is an about description of your repository.

All Contributors

Built With

This section should list any major frameworks that you built your project using. Leave any add-ons/plugins for the acknowledgements section. Here are a few examples.

Features

  1. Publish
  • PublishAsync
  1. Subscribe
  • Subscribe
  • UnSubscribe
  • StarProcessingAsync
  • StopProcessingAsync

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Prerequisites

.net 6 Visual Studio or Visual Studio Code

Installation

Installation instructions Events Abstraction by running:

  1. Clone the repo
git clone --recurse-submodules https://github.com/ERNI-Academy/assets-events-abstraction.git

Important Note
All implementations heavly depends on Microsoft Options Pattern for configurations. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0 So it is expected a proper configuration in order to work take a look at the samples to see how to configure each publisher/subscriber All implementatins also depends on Microsoft logging. See https://docs.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line

  1. Publish Basic use
class MyEvent : EventBase
{
	public string MyCustomProperty { get; set; }
}

var @event = new MyEvent { MyCustomProperty = "hi" };

//you can choose between many impl
IEventPublisher publisher = new ErniAcademy.Events.EventGrid.EventGridPublisher();//args ommited for simplicity
IEventPublisher publisher = new ErniAcademy.Events.Redis.RedisPublisher();//args ommited for simplicity
IEventPublisher publisher = new ErniAcademy.Events.ServiceBus.ServiceBusPublisher();//args ommited for simplicity
IEventPublisher publisher = new ErniAcademy.Events.StorageQueues.StorageQueuePublisher();//args ommited for simplicity

//just publish your event 
await publisher.PublishAsync(@event);
  1. Publish Depency injection (ServiceCollection)
class MyEvent : EventBase
{
	public string MyCustomProperty { get; set; }
}

//when configuring your ServiceCollection use the extension methods defined in each library for easy of use. 
//This sample is provided with no arguments, take a look on the extensions to see the rest of the arguments, like IConfiguration, ISerializer etc.
services.AddEventsPublisherEventGrid();//args ommited for simplicity
services.AddEventsPublisherRedis();//args ommited for simplicity
services.AddEventsPublisherServiceBus();//args ommited for simplicity
services.AddEventsPublisherStorageQueues();//args ommited for simplicity

//then just inject IEventPublisher directly in your classes

class MyService
{
  private readonly IEventPublisher _publisher;

  public MyService(IEventPublisher publisher)
  {
    _publisher = publisher;
  }

  public async Task SomeMethod()
  {
      //... some logic
      
      var @event = new MyEvent { MyCustomProperty = "hi" };

      //just publish your event 
      await _publisher.PublishAsync(@event);
  }
}
  1. Subscribe Basic use
class MyEvent : EventBase
{
	public string MyCustomProperty { get; set; }
}

//you can choose between many impl
IEventPublisher subscriber = new ErniAcademy.Events.Redis.RedisSubscriber();//args ommited for simplicity
IEventPublisher subscriber = new ErniAcademy.Events.ServiceBus.ServiceBusSubscriber();//args ommited for simplicity
IEventPublisher subscriber = new ErniAcademy.Events.StorageQueues.StorageQueueSubscriber();//args ommited for simplicity

//first subscribe to an @event by passing a handler to subscribe method
subscriber.ProcessEventAsync += MyProcessor;

private Task MyProcessor(MyEvent @event)
{
//Process @event
}

//start processing events 
await subscriber.StarProcessingAsync();

//and when you are done
await subscriber.StopProcessingAsync();
  1. Subscribe Depency injection (ServiceCollection)
class MyEvent : EventBase
{
	public string MyCustomProperty { get; set; }
}

//when configuring your ServiceCollection use the extension methods defined in each library for easy of use. 
//This sample is provided with no arguments, take a look on the extensions to see the rest of the arguments, like IConfiguration, ISerializer etc.
services.AddEventsSubscriberRedis<MyEvent>();//args ommited for simplicity
services.AddEventsSubscriberServiceBus<MyEvent>();//args ommited for simplicity
services.AddEventsSubscriberStorageQueues<MyEvent>();//args ommited for simplicity

//then just inject IEventPublisher directly in your classes

class MyService
{
  private readonly IEventSubscriber<DummyEvent> _subscriber;

  public MyService(IEventSubscriber<DummyEvent> subscriber)
  {
    _subscriber = subscriber;
  }

  public async Task SomeMethod()
  {
      //first subscribe to an @event by passing a handler to subscribe method
      _subscriber.ProcessEventAsync += MyProcessor;

      //start processing events 
      await _subscriber.StarProcessingAsync();

      //and when you are done
      await _subscriber.StopProcessingAsync();
  }

  private Task MyProcessor(MyEvent @event)
  {
  //Process @event
  }
}

Contributing

Please see our Contribution Guide to learn how to contribute.

License

MIT

(LICENSE) Β© 2022 ERNI - Swiss Software Engineering

Code of conduct

Please see our Code of Conduct

Stats

Alt

Follow us

Twitter Follow Twitch Status YouTube Channel Views Linkedin

Contact

πŸ“§ esp-services@betterask.erni

Contributors ✨

Thanks goes to these wonderful people (emoji key):


omaramalfi

πŸ’» πŸ–‹ πŸ“– 🎨 πŸ€” 🚧 ⚠️ πŸ’‘ πŸ‘€

This project follows the all-contributors specification. Contributions of any kind welcome!