Giter Club home page Giter Club logo

xer.cqrs.eventstack's Introduction

Build

Branch Status
Master Build status
Dev Build status

Table of contents

Overview

Simple CQRS library

This project composes of components for implementing the CQRS pattern (Event Handling). This library was built with simplicity, modularity and pluggability in mind.

Features

  • Send event to registered event handlers.
  • Provides simple abstraction for hosted event handlers which can be registered just like an regular event handler.
  • Multiple ways of registering event handlers:
    • Simple handler registration (no IoC container).

    • IoC container registration

      • achieved by creating implementations of IContainerAdapter or using pre-made extensions packages for supported containers:
        • Microsoft.DependencyInjection

          NuGet

        • SimpleInjector

          NuGet

        • Autofac

          NuGet

    • Attribute registration

Installation

You can simply clone this repository, build the source, reference the dll from the project, and code away!

Xer.Cqrs.EventStack library is available as a Nuget package:

NuGet

To install Nuget packages:

  1. Open command prompt
  2. Go to project directory
  3. Add the packages to the project:
    dotnet add package Xer.Cqrs.EventStack
  4. Restore the packages:
    dotnet restore

Getting Started

(Samples are in ASP.NET Core)

Sample Event and Event Handlers

public class ProductRegisteredEvent
{
    public int ProductId { get; }
    public string ProductName { get; }

    public ProductRegisteredEvent(int productId, string productName)
    {
        ProductId = productId;
        ProductName = productName;
    }
}

// Sync event handler
public class ProductRegisteredEventHandler : IEventHandler<ProductRegisteredEvent>
{
    public void Handle(ProductRegisteredEvent @event)
    {
        System.Console.WriteLine($"ProductRegisteredEventHandler handled {@event.GetType()}.");
    }
}

// Async event handler
public class ProductRegisteredEmailNotifier : IEventAsyncHandler<ProductRegisteredEvent>
{
    public Task HandleAsync(ProductRegisteredEvent @event, CancellationToken ct = default(CancellationToken))
    {
        System.Console.WriteLine($"Sending email notification...");
        return Task.CompletedTask;
    }
}

Event Handler Registration

Before we can delegate any events, first, we need to register our event handlers. There are several ways to do this:

1. Simple Registration (No IoC container)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{            
    ...
    // Repository.
    services.AddSingleton<IProductRepository, InMemoryProductRepository>();

    // Register event delegator.
    services.AddSingleton<EventDelegator>((serviceProvider) =>
    {
        // Allows registration of a multiple message handlers per message type.
        var registration = new MultiMessageHandlerRegistration();
        registration.RegisterEventHandler<ProductRegisteredEvent>(() => new ProductRegisteredEventHandler());
        registration.RegisterEventHandler<ProductRegisteredEvent>(() => new ProductRegisteredEmailNotifier());
        
        return new EventDelegator(registration.BuildMessageHandlerResolver());
    });
    ...
}
2. Container Registration
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{            
    ...
    // Repository.
    services.AddSingleton<IProductRepository, InMemoryProductRepository>();
    
    // Register event handlers to the container. 
    // The AddCqrs extension method is in Xer.Cqrs.Extensions.Microsoft.DependencyInjection package.
    services.AddCqrs(typeof(ProductRegisteredEventHandler).Assembly);
    ...
}

Delegating Events to Event Handlers

After setting up the event delegator in the Ioc container, events can now be delegated by simply doing:

...
private readonly EventDelegator _eventDelegator;

public ProductsController(EventDelegator eventDelegator)
{
    _eventDelegator = eventDelegator;
}

[HttpGet("{productId}")]
public async Task<IActionResult> Notify(ProductRegisteredEventDto model)
{
    await _eventDelegator.SendAsync(new ProductRegisteredEvent(model.ProductId, model.ProductName))
    return Accepted();
}
...

xer.cqrs.eventstack's People

Contributors

joel-jeremy avatar mvput avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.