我正在尝试按照以下模式遵循 DDD。
Controller-----DataContract----> Domain Layer (DDD)
Controller-----Domain Object---> Repository---Entity--->EntityFramework
如您在上图中所见,域层 独立做出业务决策,但就我而言,大部分业务决策都是即时做出的。例如,
if(Account Number Associated?)
Load CustomerDetails //A database call is needed
....
.....
if(Has customer another loan)
.....
.....
Load other loan details //A database call is needed
.....
.....
if(Was that repaid?)
....
....
Load collateral details //A database call is needed
.....
.....
Calculate collateral details and return.
else
Load other data //A database call is needed
else
Load other data //A database call is needed
else
Load other data //A database call is needed
正如您在上面的示例中看到的,应用程序通过进行数据库调用来即时做出大量业务决策。由于 Domain Layer 不应依赖于 Repository Layer,我不知道如何进行。
我可能会使用应用程序服务 进行数据库调用,但是域层 中不会有任何逻辑。所有逻辑都将进入应用程序服务。
请帮助我。
- Pandas
最佳答案
至少有三种可能的出路
1) 将您的存储库设计为一次加载整个聚合。这种方法立即为域模型提供它可能需要的所有状态,而不是尝试按需加载状态。
2) 在应用服务中运行查询,并将数据传递给领域模型。理想情况下,您提前执行此操作(以便您对域模型进行单次调用),但是当这没有意义时,您让域模型告诉应用程序服务需要什么数据,然后应用程序服务发现数据并返回。
3) 将存储库传递到域模型中,使其能够读取所需的数据。这本质上是“域服务”模式,但用于访问数据存储。
在此设计中,域模型定义存储库接口(interface),而应用程序提供实现。换句话说,我们正在使用服务提供者模式来保持依赖箭头指向正确的方向。
关于domain-driven-design - DDD - 业务决策基于数据库逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247826/