c# - 能否使用设计模式重构此 MVC 代码?

标签 c# asp.net-mvc asp.net-mvc-3 events design-patterns

我的 ASP.NET MVC 3 站点上到处都是这样的 Controller 代码:

[HttpPost]
public ActionResult Save(PostViewModel viewModel)
{
   // VM -> Domain Mapping. Definetely belongs here. Happy with this.
   var post = Mapper.Map<PostViewModel, Post>(viewModel);

   // Saving. Again, fine. Controllers job to update model.
   _postRepository.Save(post);

   // No. Noooo..caching, thread spawning, something about a user?? Why....
   Task.Factory.StartNew(() => {
       _cache.RefreshSomeCache(post);
       _cache2.RefreshSomeOtherCache(post2);
       _userRepository.GiveUserPoints(post.User);
       _someotherRepo.AuditThisHappened();
   });

   // This should be the 3rd line in this method.
   return RedirectToAction("Index");
}

基本上,我指的是线程 block 中的代码。所有事情都需要发生,但用户不需要等待它们(后台线程的好例子,对吧?)。

需要说明的是,我在整个站点都使用了缓存(常规 ASP.NET 数据缓存),其中大部分都有“不过期”缓存策略,所以我在需要时手动将其逐出(如上所示)。

用户部分基本上是为用户做某事(比如 Stack)提供代表。

让我们回顾一下:我们将缓存、用户信誉处理、审计合而为一。确实不属于一个地方。因此存在当前代码的问题,以及试图弄清楚如何将其移走的问题。

我想重构它的原因有几个:

  1. 难以进行单元测试。多线程和单元测试并没有很好地发挥作用。
  2. 可读性。很难读。凌乱。
  3. 建议零售价。控制者做的/知道的太多。

我解决了 1) 通过将线程生成代码包装到一个接口(interface)中,然后只是模拟/伪造它。

但是我想做一些模式,我的代码看起来像这样:

[HttpPost]
public ActionResult Save(PostViewModel viewModel)
{
   // Map.
   var post = Mapper.Map<PostViewModel, Post>(viewModel);

   // Save.
   _postRepository.Save(post);

   // Tell someone about this.
   _eventManager.RaiseEvent(post);

   // Redirect.
   return RedirectToAction("Index");
}

基本上,让“其他东西”负责使用react,而不是 Controller 。

我听过/读过有关任务、命令、事件等的内容,但还没有看到在 ASP.NET MVC 空间中实现的内容。

我的第一个想法是创建某种“事件管理器”。但后来我想,这是去哪儿了?在域中?那么它如何处理与缓存的交互,这是一个基础设施问题。然后是线程,这也是一个基础设施问题。如果我想同步而不是异步怎么办?是什么做出了这个决定?

我不想将所有这些逻辑堆放在其他地方。理想情况下,它应该被重构为可管理和有意义的组件,而不是转移责任,如果这有意义的话。

有什么建议吗?

最佳答案

First thoughts would tell me to create some kind of "event manager". But then i thought, where does this go? In the domain?

这就是我解决问题的方式。我将事件管理器视为基础设施。但实际事件属于域。

Well then how does it handle interactions with the cache, which is an infrastructure concern. And then threading, which is also an infrastructure concern. And what if i want to do is synchronously, instead of async? What makes that decision?

异步很好,但会使事务处理变得复杂。如果您使用 IoC 容器,您已经拥有定义明确的范围和可在事件传播期间使用的事务。

恕我直言,如果订阅者知道它的事件处理需要时间,就可以安排/线程化它的任务。

建议的解决方案:

使用您的 IoC 容器发布事件。我会让存储库发布事件(PostUpdatedEntityUpdated,具体取决于您要对事件执行的操作)而不是 Controller (以减少代码重复)。

我为 autofac 做了一个 IoC 实现,它允许你:

DomainEventDispatcher.Current.Dispatch(new EntityUpdated(post));

订阅:

public class CacheService : IAutoSubscriberOf<EntityUpdated>
{
    public void Handle(EntityUpdated domainEvent) {};
}

https://github.com/sogeti-se/Sogeti.Pattern/wiki/Domain-events

典型用法

  1. 实现 IServiceResolver (为您的容器)
  2. 分配它:ServiceResolver.Assign(new yourResolver(yourContainer))
  3. 按说明使用 here .

关于c# - 能否使用设计模式重构此 MVC 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9597529/

相关文章:

c# - 在新的 VS Studio 2017 ASP.NET MVC 5.2.3 项目中使用 C# 7 功能时出现编译错误

javascript - 当从 MVC 的下拉列表中选择一个值时出现一个文本框

asp.net-mvc-3 - ViewBag.Title 错误

c# - 使用 iText 7 创建的可见签名未在 chrome 中显示

c# - 数组与数组列表的显着差异?

c# - WCF REST 服务中的 CORS 支持

asp.net-mvc - 未经验证的 IValueProvider.GetValue

c# - SSRS : Master-detail report with two datasources

asp.net-mvc-3 - MVC3 Ajax 调用 Controller

asp.net-mvc - 我可以在这里使用路线约束吗?