Raising domain events for multiple subscribers

I declare that I look into the domain event template and read a lot of resources on this subject, but I cannot find a good implementation method for our requirements. We basically have a Service / Domain layer that wraps a read / write repository layer with a simplified CQRS implementation. We have an ASP.NET Mvc application that consumes this level of service / domain. The entire application is associated with Autofac, and I would like this to happen:

When a news item is created by calling "CreateNews" in the register of the service layer, that event should be raised like this:

public void CreateNews(Domain.Entities.News.NewsBO news) { ValidateBusinessObject(news); var entityNews = AutoMapper.Mapper.Map<Repositories.Entities.News.News>(news); NewsCommandRepository.Create(entityNews); _domainEventManager.Register<NewsCreatedDomainEvent>(x => x.News = news); } 

This happens in a transaction, and I don’t really want to raise the event until the save is completed, so in our method of saving changes I want to do this:

 public void SaveChanges() { _repoCommandManager.SaveChanges(); _domainEventManager.RaiseEvents(); } 

Then in our ASP.NET Mvc application, I want to have an IHandler implementation that looks like this:

 public class NewsCreatedDomainEventCacheHandler : IHandles<Project.Services.Domain.Events.News.NewsCreatedDomainEvent> { public void Handle(Services.Domain.Events.News.NewsCreatedDomainEvent @event) { // In here we would update the cache or something else particular to the web layer } } 

I cannot figure out how to promote this event from the save method and invoke the implementation in a Web.Mvc application.

Any suggestions would be appreciated.

+2
source share
1 answer

I think I have an example of how to do this for you, and I use MVC and AutoFac too! In my specific example, I concentrate on the separation of commands / requests, but at the same time I had to implement a domain event pattern.

Read this blog post first to get a general overview of how everything goes together and what the code looks like: http://www.nootn.com.au/2013/03/command-query-separation-to-better.html

Therefore, I would recommend installing the DotNetAppStarterKit.Web.Mvc NuGet package , and then take a look at the Global.asax file to register all the components you need. You can view the SampleMvc app for things like Event Subscribers .

Hope this helps you quickly and quickly. You can simply use the publisher / subscriber parts of the DotNetAppStarterKit event without using commands and queries.

+3
source