我正在(重新)设计大型应用程序,我们使用基于DDD的多层架构。

我们有带有数据层(存储库的实现)、领域层(域模型和接口的定义——存储库、服务、工作单元)、服务层(服务的实现)的MVC。到目前为止,我们在所有层上使用域模型(主要是实体),并且仅将dto用作视图模型(在控制器中,服务返回域模型,控制器创建视图模型,并将其传递给视图)。

I'v read countless articles about using, not using, mapping and passing DTOs. I understand that there's no any definitive answer, but I'm not sure if it's ok or not returning domain models from services to controllers. If I return domain model, it's still never passed to the view, since controller always creates view-specific view model - in this case, it seem legit. On the other hand, it doesn't feel right when domain model leaves business layer (service layer). Sometimes service needs to return data object that wasn't defined in the domain and then we either have to add new object to the domain that isn't mapped, or create POCO object (this is ugly, since some services return domain models, some effectively return DTOs).

The question is - if we strictly use view models, is it ok to return domain models all the way to controllers, or should we always use DTOs for communication with service layer? If so, is it ok to adjust domain models based on what services need? (Frankly I don't think so, since services should consume what domain has.) If we should strictly stick to DTOs, should they be defined in service layer? (I think so.) Sometimes it's clear that we should use DTOs (e.g., when service performs lot of business logic and creates new objects), sometimes it's clear that we should use just domain models (e.g., when Membership service returns anemic User(s) - it seems it wouldn't make much sense to create DTO that is the same as domain model) - but I prefer consistency and good practices.

文章域vs DTO vs ViewModel -如何以及何时使用它们?(以及其他一些文章)与我的问题非常相似,但它没有回答这个问题。我应该用EF在存储库模式中实现dto吗?也类似,但它不处理DDD。

免责声明:我不打算仅仅因为任何设计模式的存在而使用它,另一方面,我想使用好的设计模式和实践,因为它有助于设计应用程序作为一个整体,有助于分离关注点,即使使用特定的模式不是“必要的”,至少目前是这样。


当前回答

如果您返回域模型的一部分,它将成为契约的一部分。合同是很难改变的,因为你上下文之外的事情都依赖于它。因此,您将使部分域模型难以更改。

领域模型的一个非常重要的方面是它很容易更改。这使我们能够灵活地适应领域不断变化的需求。

其他回答

我建议分析这两个问题:

Are your upper layers (i.e. view & view models / controllers) consuming the data in a different way of what the domain layer exposes? If there is a lot of mapping being done or even logic involved I'll suggest revisiting your design: it should probably be closer to how the data is actually used. How likely is it that you deeply change your upper layers? (e.g. swapping ASP.NET for WPF). If this is highly unlike and your architecture is not very complex, you may be better off exposing as many domain entities as you can.

恐怕这是一个相当广泛的话题,它实际上涉及到您的系统有多复杂及其需求。

It seems that your application is big and complex enough as you have decided to go through DDD approach. Don't return your poco entities or so called domain entities and value objects in you service layer. If you want to do this then delete your service layer because you don't need it anymore! View Model or Data transfer objects should live in Service layer because they should map to domain model members and vice versa. So why do you need to have DTO? In complex application with lots of scenarios you should separate the concerns of domain and you presentation views, a domain model could be divided into several DTO and also several Domain models could be collapsed into a DTO. So it's better to create your DTO in layered architecture even it would be the same as your model.

我们应该总是使用dto与服务层通信吗? 是的,你必须通过你的服务层返回DTO,因为你必须在服务层中通过域模型成员与存储库对话,并将它们映射到DTO并返回到MVC控制器,反之亦然。

是否可以根据服务需求调整域模型? 服务只与存储库、域方法和域服务对话,您应该根据自己的需求解决域内的业务,而不是告诉域需要什么。

如果我们应该严格遵循dto,那么它们应该在服务层定义吗?是的,尝试稍后在服务中使用DTO或ViewModel,因为它们应该映射到服务层中的域成员,并且在应用程序的控制器中放置DTO不是一个好主意(尝试在服务层中使用请求响应模式),干杯!

根据我的经验,你应该做些实际的事情。“最好的设计是最简单的设计”——爱因斯坦。有了这样的思想……

如果我们严格使用视图模型,是否可以将域模型一直返回到控制器,或者我们应该始终使用dto与服务层通信?

当然没关系!如果你有域实体,DTO和视图模型,那么包括数据库表,你在应用程序中重复的所有字段在4个地方。我曾参与过一些大型项目,其中领域实体和视图模型工作得很好。唯一的例外是,如果应用程序是分布式的,而服务层位于另一台服务器上,在这种情况下,由于序列化的原因,需要通过网络发送dto。

如果是,是否可以根据服务需求调整域模型?(坦白地说,我不这么认为,因为服务应该消费域拥有的东西。)

一般来说,我同意并说不,因为域模型通常是业务逻辑的反映,通常不是由该逻辑的消费者塑造的。

如果我们应该严格遵循dto,那么它们应该在服务层定义吗?(我想是的。)

如果你决定使用它们,我同意并说是的,服务层是一个完美的地方,因为它在一天结束时返回dto。

好运!

如果您返回域模型的一部分,它将成为契约的一部分。合同是很难改变的,因为你上下文之外的事情都依赖于它。因此,您将使部分域模型难以更改。

领域模型的一个非常重要的方面是它很容易更改。这使我们能够灵活地适应领域不断变化的需求。

In my experience, unless you are using an OO UI pattern (like naked objects), exposing the domain objects to the UI is a bad idea. This because as the application grows, the needs from the UI change and force your objects to accommodate those changes. You end up serving 2 masters: UI and DOMAIN which is a very painful experience. Believe me, you don't want to be there. The UI model has the function of communicating with the user, the DOMAIN model to hold the business rules and the persistence models deals with storing data effectively. They all address different needs of the application. I'm in the middle of writing a blog post about this, will add it when it's done.