MVC DDD:是否确定使用存储库连同控制器服务? [英] MVC DDD: Is it OK to use repositories together with services in the controller?

查看:161
本文介绍了MVC DDD:是否确定使用存储库连同控制器服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中的大多数服务code的时候我会是这样的:

 公共SomeService:ISomeService
{
    ISomeRepository someRepository;
    公共DO(INT ID)
    {
        someRepository.Do(ID);
    }
}

所以它有点多余

所以我开始在控制器中直接使用库

这是好吗?有一些建筑正在做这样的?


解决方案

  

您失去之间都存在业务逻辑的能力。


我不同意这一点。

如果业务逻辑是它应该是 - 在域模型,在控制器然后调用回购(或更好 - 对于使用模型绑定)来获得聚合根并在其上调用方法似乎完全没有给我

当有涉及太多的技术细节,应用服务应该使用什么会陷入困境控制器。



  

我见过几个人提用模型来粘合剂最近调入回购。哪里是这个疯狂的想法来自哪里?


我相信我们在这里所说的2个不同的东西。我怀疑你的模型绑定同时使用模型作为视图模型也并结合从UI改变的值直接右后卫它(这是不是一件坏事本身,在某些情况下,我会去那条路)表示。

我的'模型绑定是实现的 IModelBinder ',即发生在构造函数库(其被注入,所以 - 如果可以,我们需要用一些基本的组成缓存扩展),并使用它之前被称为动作来取得聚合根和替换内部编号的Guid ID 字符串蛞蝓什么操作参数与真正的域对象。结合与输入视图模型参数让我们少写code。事情是这样的:

 公众的ActionResult ChangeCustomerAddress
 (客户C,ChangeCustomerAddressInput INP){
  c.ChangeCustomerAddress(inp.NewAddress);
  返回RedirectToAction(详细信息,新的{inp.Id});
}

在我的实际code这是一个有点更复杂的原因,它包括的ModelState验证和可能从域模型内被抛出一些异常处理(提取到控制器扩展方法重复使用)。但没有更多。到目前为止 - 最长的控制器动作〜10行代码

您可以看到工作实现(相当复杂,(对我来说)不必要的复杂)<一个href=\"http://$c$ccampserver.$c$cplex.com/SourceControl/changeset/view/4755c1386bff#src%2fUI%2fBinders%2fKeyedModelBinder.cs\"相对=nofollow>这里。


  

您只是在做的LINQ CRUD应用to SQL或试图与真正的域逻辑的东西吗?


你可以(希望)看,这种做法实际上几乎迫使我们对移动基于任务的应用程序,而不是基于CRUD之一。


  

这样做在你的服务层的所有数据的访问和使用国际奥委会你可以得到许多像无形的缓存,事务管理,而我不能想象你与模型得到的粘合剂成分的组成方便的AOP的好处。


...并具有新的抽象层,邀请我们与域逻辑混合基础设施和失去域模型隔离。


  

请赐教。


我不知道如果我这样做。我不认为我自己悟。 :)


这里是我目前的模型绑定基类。 下面是从我目前的项目负责人的行动之一。和这里的业务逻辑的缺乏。

most of the time in the service code I would have something like this:

public SomeService : ISomeService
{
    ISomeRepository someRepository;
    public Do(int id)
    {
        someRepository.Do(id);
    }
}

so it's kinda redundant

so I started to use the repositories directly in the controller

is this ok ? is there some architecture that is doing like this ?

解决方案

You lose the ability to have business logic in between.

I disagree with this one.

If business logic is where it should be - in domain model, then calling repo in controller (or better - use model binder for that) to get aggregate root and call method on it seems perfectly fine to me.

Application services should be used when there's too much technical details involved what would mess up controllers.


I've seen several people mention using model binders to call into a repo lately. Where is this crazy idea coming from?

I believe we are talking about 2 different things here. I suspect that Your 'model binder' means using model simultaneously as a view model too and binding changed values from UI directly right back to it (which is not a bad thing per se and in some cases I would go that road).

My 'model binder' is a class that implements 'IModelBinder', that takes repository in constructor (which is injected and therefore - can be extended if we need caching with some basic composition) and uses it before action is called to retrieve aggregate root and replace int id or Guid id or string slug or whatever action argument with real domain object. Combining that with input view model argument lets us to write less code. Something like this:

public ActionResult ChangeCustomerAddress
 (Customer c, ChangeCustomerAddressInput inp){
  c.ChangeCustomerAddress(inp.NewAddress);
  return RedirectToAction("Details", new{inp.Id});
}

In my actual code it's a bit more complex cause it includes ModelState validation and some exception handling that might be thrown from inside of domain model (extracted into Controller extension method for reuse). But not much more. So far - longest controller action is ~10 lines long.

You can see working implementation (quite sophisticated and (for me) unnecessary complex) here.

Are you just doing CRUD apps with Linq To Sql or trying something with real domain logic?

As You can (hopefully) see, this kind of approach actually almost forces us to move towards task based app instead of CRUD based one.

By doing all data access in your service layer and using IOC you can gain lots of benefits of AOP like invisible caching, transaction management, and easy composition of components that I can't imagine you get with model binders.

...and having new abstraction layer that invites us to mix infrastructure with domain logic and lose isolation of domain model.

Please enlighten me.

I'm not sure if I did. I don't think that I'm enlightened myself. :)


Here is my current model binder base class. Here's one of controller actions from my current project. And here's "lack" of business logic.

这篇关于MVC DDD:是否确定使用存储库连同控制器服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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