提高域事件对于多用户 [英] Raising Domain Events For Multiple Subscribers

查看:152
本文介绍了提高域事件对于多用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指出来调查域事件模式,也读了不少资源,关于这个问题,但是我无法找到实施我们的要求的一个很好的方式。基本上我们有读取/用一个简单的CQRS实现写它包装库层的服务/业务层。我们有一个ASP.NET MVC应用程序消耗这个服务/业务层。整个应用程序与Autofac系在一起,我想发生这样的是以下内容:

I am stating to look into the Domain Events pattern and have read a lot of resources on the subject but I cannot find a good way of implementing for our requirements. Basically we have a Service/Domain layer which wraps the repository layer for reads/writes with a simplistic CQRS implementation. We have an ASP.NET Mvc application which consumes this service/domain layer. The whole application is tied together with Autofac and what I would like to happen is for the following:

在一个新闻条目是通过调用说:CreateNews上的服务创建层注册该事件将需要筹集如此:

When a news item is created by calling say "CreateNews" on the service layer register that an event will need to be raised as so:

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 is all happening in a transaction and I don't want to actually raise the event until the save is completed so in our save changes method I want to do this:

public void SaveChanges()
{
  _repoCommandManager.SaveChanges();

  _domainEventManager.RaiseEvents();
}

然后在我们的ASP.NET MVC应用程序我想有一个实现IHandler它看起来像这样:

Then in our ASP.NET Mvc application I want to have an implementation of an IHandler which 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
    }
}

我无法弄清楚如何去提高这个从保存方法事件,并呼吁在Web.Mvc应用程序的执行情况。

I cannot figure out how to go about raising this event from the save method and calling the implementation in the Web.Mvc application.

任何建议,将不胜感激。

Any suggestions would be appreciated.

推荐答案

我觉得我有如何做到这一点的一个例子,我碰巧使用MVC和AutoFac也!在我的具体的例子,我专注于命令/查询分离,但在这样做,所以我不得不执行域事件模式。

I think I have an example of how to do this for you and I happen to be using MVC and AutoFac also! In my specific example I am concentrating on Command/Query separation but in doing so I had to implement a domain event pattern.

首先有这个博客帖子的读取,所以你得到的东西怎么挂在一起是什么代码看起来像一个概述和:
http://www.nootn.com.au/2013/03/command-query-separation-to-better.html

First have a read of this blog post so you get an overview of how things hang together and what the code looks like: http://www.nootn.com.au/2013/03/command-query-separation-to-better.html

因此,我建议在安装 DotNetAppStarterKit.Web.Mvc的NuGet包,再看看在 Global.asax文件如何到你需要的所有组件注册。您可以仔细阅读喜欢的事情事件订阅中的应用SampleMvc

So I would recommend installing the DotNetAppStarterKit.Web.Mvc NuGet package, then take a look at the Global.asax file for how to register all the components you will need. You can peruse the SampleMvc application for things like Event Subscribers.

我希望这有助于和让您能和去快。你可以只用DotNetAppStarterKit的事件发布/订阅的部分,而无需使用命令和查询。

I hope this helps and gets you up and going quickly. You can just use the event publisher/subscriber parts of DotNetAppStarterKit without using commands and queries.

这篇关于提高域事件对于多用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆