简体   繁体   English

DDD 为什么要域 model 定义基础设施的接口

[英]DDD why should domain model define interfaces for infrastructure

While studying DDD I'm wondering why the Domain model need to define interfaces for the Infrastructure layer.在研究 DDD 时,我想知道为什么域 model 需要为基础设施层定义接口。

From my reads I got that a high level (domain model, application service, domain service) defines interfaces that need to be implemented by a lower layer (infrastructure).从我的阅读中我得到一个高层(域 model,应用程序服务,域服务)定义了需要由较低层(基础设施)实现的接口。 Simple.简单的。

From this idea I think it makes sense that an application level (in a high level) defines interfaces for a lower one (infrastructure) because the application level will use infrastructure components (A repository is a usual client of the applicaiton layer) but doesn't want to be tied to any special implementation.从这个想法来看,我认为应用程序级别(高层)为较低级别(基础架构)定义接口是有道理的,因为应用程序级别将使用基础架构组件(存储库是应用程序层的常见客户端)但不不想被任何特殊的实现所束缚。

However, this is confusing when I see in some books a domain level defining infrastructure interfaces because a domain model will not use ever a repository because we want our domain model "pure".然而,当我在一些书中看到定义基础结构接口的域级别时,这令人困惑,因为域 model 永远不会使用存储库,因为我们希望我们的域 model “纯”。

Am I missing something?我错过了什么吗?

While studying DDD I'm wondering why the Domain model need to define interfaces for the Infrastructure layer. 在研究DDD时,我想知道为什么Domain模型需要为基础结构层定义接口。

It doesn't really -- that's part of the point. 并非如此-这就是重点。

The domain model defines the interfaces / contracts that it needs to do work, with the promise of happily working with any implementation that conforms to the contract. 域模型定义了需要工作的接口/合同, 承诺可以愉快地使用符合合同的任何实现。

So you can choose to implement the interface in your application component, or in the infrastructure component, or where ever makes sense. 因此,您可以选择在应用程序组件,基础结构组件或任何有意义的地方实现接口。

Note the shift in language from "layer" to "component". 注意语言从“层”到“组件”的转变。 Layers may be too simplistic to work -- see Udi Dahan 2007 . 层次可能太简单而无法工作-参见Udi Dahan 2007

I came across the same question myself.我自己遇到了同样的问题。 From my understanding, the reason behind this is that you want the application to only consume objects/interfaces defined in the Domain Model. It also helps to keep one repository per aggregate since they sit next to each other in the same layer.据我了解,这背后的原因是您希望应用程序仅使用域 Model 中定义的对象/接口。它还有助于为每个聚合保留一个存储库,因为它们在同一层中彼此相邻。

The fact that the Application layer has a reference to the Infrastructure layer is only for the purpose of dependency injection. Application 层引用 Infrastructure 层的事实只是为了依赖注入的目的。 Your Application layer services should call these interfaces exposed in the Domain layer, get Domain Objects (POCOs), do things and possibly call interfaces again;你的应用层服务应该调用这些暴露在域层的接口,获取域对象(POCOs),做一些事情并可能再次调用接口; for example to commit a transaction.例如提交事务。 This is why for example the Unit of Work pattern exposes its action through a Domain Layer interface.这就是为什么例如工作单元模式通过领域层接口公开其操作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM