Skip to content


Subversion checkout URL

You can clone with
Download ZIP


sapiens edited this page · 8 revisions
Clone this wiki locally



  1. Define an event

    public class OrderPlacedEvent:IDomainEvent
    public class OrderPlacedEvent:DomainEventBase<Order>
        public OrderPlacedEvent(Order data) : base(data)

    Every domain event must implement IDomainEvent or extend DomainEventBase

  2. Define a handler

      public class OrderPlacedEventHandler:DomainEventHandlerBase<OrderPlacedEvent>
        public override void Handle(OrderPlacedEvent ev)
          //handle event
  3. Create the main domain events manager (as a static variable or via your favorite DI container)

      static IApplicationDomainEventsManager DomainEvents=new ApplicationDomainEventsManager()

    ApplicationDomainEventsManager is designed to be used as a singleton and it's thread safe.

Note: Starting with version 1.1.0 you can use the DomainEvents helper which is a static facade for a LocalDomainEventsManager. However, especially for web apps where handlers are valid only for certain requests, I recommend NOT to use it. It's more maintainable to use a DI container and a 'standard' LocalDomainEventsManager which can be injected into the Controller.


  • Register an event handler

    var subscription=DomainEvents.RegisterHandler<MyEvent>(x=> 
                                          {//inline handler
    RegisterHandler(new OrderedPlacedEventHandler()) //the handler is automatically registered for the OrderPlacedEvent

    Every time a handler is registered, a disposable object is returned. Diposing it ends the subscription.

  • Unregister a handler

  • Publish an event

    DomainEvents.Publish(new OrderPlacedEvent(myOrder));

    Events are handled from the most specific type to the most abstract type.

    DomainEvents.RegisterHandler<MyEvent>(new MyEventHandler());
    DomainEvents.Publish(new MyEvent());

    MyEvent instance will be handled first by the MyEventHandler and second by the lambda method handler, because MyEvent inherits from IDomainEvent


For those cases where you want not to publish events immediately, you can use the buffering feature


All the events will be stored until EndBuffering is called. Then all the buffered events will be published.

An alternate usage:

using (events.BeginBuffering())

Web applications

In a web app, besides a global domain events manager, you may want a 'local' domain events manager, to handle domain events for a single request. This means that each request will get its own domain events manager which will live until the request ends. You can do that using the LocalDomainEventsManager class, but be aware that this class is NOT thread safe.

var domainEvents=new LocalDomainEventsManager();
var domainEvents= DomainEvents.SpawnLocal();

If you create the local domain events manager via the application domain events manager, any event published by the local manager will be also published by the application manager.

Both ApplicationDomainEventsManager and LocalDomainEventsManager implement the IDomainEventsManager interface which can be used to inject the manager anywhere needed.

Something went wrong with that request. Please try again.