Giter Site home page Giter Site logo

techninjalabs / domainevents Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 61 KB

DomainEvents is .Net library to implement transactional events in domain model.

License: MIT License

C# 100.00%
domain-events domain-events-platform event-pub-sub domian-events-pub-sub event-publisher publisher-subscriber

domainevents's Introduction

ninja DomainEvents v3.0.0

NuGet version License: MIT CI GitHub Release CodeQL .Net 6.0

Library to help implement transactional events in domain bounded context.

Use domain events to explicitly implement side effects of changes within your domain. In other words, and using DDD terminology, use domain events to explicitly implement side effects across multiple aggregates.

What is a Domain Event?

An event is something that has happened in the past. A domain event is, something that happened in the domain that you want other parts of the same domain (in-process) to be aware of. The notified parts usually react somehow to the events. The domain events and their side effects (the actions triggered afterwards that are managed by event handlers) should occur almost immediately, usually in-process, and within the same domain. It's important to ensure that, just like a database transaction, either all the operations related to a domain event finish successfully or none of them do.

Figure below shows how consistency between aggregates is achieved by domain events. When the user initiates an order, the Order Aggregate sends an OrderStarted domain event. The OrderStarted domain event is handled by the Buyer Aggregate to create a Buyer object in the ordering microservice (bounded context). Please read Domain Events for more details.

image

How to Define, Publish and Subscribe to an Event using DomainEvents library?

  1. Define - To implement a domain event, simply derive the event class from IDomainEvent interface.
public class CustomerCreated : IDomainEvent {
        public string Name { get; set; }
}
  1. Publish - To raise the domain event, Inject IPublisher using your favourite IoC container and call the RaiseAsync() method.
  var @event = new CustomerCreated { Name = "Ninja Sha!4h" };
  await _Publisher.RaiseAsync(@event);
  1. Subscribe - To listen to a domain event, implement IHandler<T> interface where T is the event type you intend to handle.
public class CustomerCreatedHandler : IHandler<CustomerCreated>
{
     public Task HandleAsync(CustomerCreated @event)
     {
         Console.WriteLine($"Customer created: {@event.Name}");
         .....
     }
}
  1. Example - IoC Container Registrations
public void ConfigureServices(IServiceCollection services)
{   
    // register publisher with required lifetime.
    services.AddTransient<IPublisher, Publisher>();
    
    // register all implemented event handlers.
    services.AddTransient<IHandler, CustomerCreatedHandler>();
    services.AddTransient<IHandler, OrderReceivedHandler>();
}

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.