Ton*_*imo 3 domain-driven-design ddd-repositories clean-architecture
我正在设计一个运输应用程序,并尝试使用Clean Architecture和DDD。在域层的核心深处,我们有许多可配置的业务规则。例如,有一些业务规则用于确定货运的最佳承运人,确定运输方式,确定付款类型等。每个业务规则都会从数据库中选择数据,因此我打算使用BizRule存储库。问题是,根据我对DDD原理的理解,域实体(例如,货件)不应调用存储库(例如,BizRuleRepository)。用例层应该是调用存储库的那一层。如果我采用这种方法,那么我将不得不将许多复杂的业务规则移至用例层,我不确定这是否是最佳方法。在这种情况下,设置例外并让域实体调用存储库是否有意义?先感谢您。
域实体应该调用存储库吗?
Generally speaking, no; it doesn't make sense for an entity (which is a domain concern) to be communicating directly with a repository (which is plumbing).
Evans, when organizing his book, assigned these ideas to different chapters
It's a problem of language; Repositories normally have collection or persistence semantics, which are not (typically) part of the ubiquitous language of the domain.
That said, there is a loop hole; domain services can describe data retrieval using the ubiquitous language, and delegate that work to application or infrastructure services.
So (assuming for the moment that the business rules are a domain concept), you would have a domain entity that knows which business rule it needs, and a domain service that knows how to retrieve a business rule, and then the entity that knows how to use it.
If business rules are not a domain concept, then some of the work shifts from the entity into the domain service, but the core of the pattern remains the same -- the entity passes arguments to the service, the service returns a domain value the entity understands, the entity decides how to apply that value in its current processing.
We can solve any problem by introducing an extra level of indirection
有点壳游戏;在幕后,我们仍在使用管道;但是领域模型只能看到瓷器。
当您要对域逻辑进行单元测试而又不拖累整个管道依赖关系时,这种额外的间接层可能真的很方便:您将域服务替换为测试倍数。