Using framework event dispatcher to raise domain event

557 views Asked by At

When i need to raise domain events, should i use framework specific Event Dispatcher or create my own Event Dispatcher that implemented by framework event dispatcher ? Since the framework has a really nice event dispatcher and the term of DDD said the domain layer should not dependent on any external thing, i got confused. :D

Thanks

3

There are 3 answers

2
Eben Roux On BEST ANSWER

Lately I favor returning events from domain methods and handling those in the application service layer. Since the application service layer is where you bring together all kinds of infrastructure further processing of the returned events, such as dispatching, can be handled in the application service layer.

In this way you do not need to use a dispatcher singleton or pass in any reference to a dispatcher to your domain.

1
Marco On

what i do is have my aggregate record a list of events that happen during a business transaction and within my application service i publish the event like this:

        IDomainEventPublisher publisher = new DomainEventPublisher();
        publisher
            .SubscribeTo<ConsigneeCreated>()
                .UsingDelegate(e =>
                    Console.WriteLine(string.Format("Consignee Id - {0}", e.ConsigneeId.ToString())));


        publisher.Publish(consignee.ReleaseEvents());
2
Luceos On

It's a design choice. If you want to add additional functionality to the event dispatcher or expect to be doing that soon, extend. Otherwise if you implement that exact functionality, just use the class. There is no need to extend classes and you will be always able to do so once the need arrives.