领域模型应该如何耦合?所有聚合根都应该是接口吗?

发布于 2024-10-27 13:32:08 字数 386 浏览 0 评论 0原文

我们终于构建了一个领域模型。域模型包括用于将域对象松散耦合到持久性的接口。然而,我想知道域模型对象之间应该如何耦合。

订单是否指向客户ICustomer

这篇文章提到了问题积极地解耦对象,并且似乎不鼓励“过度使用[接口]”。然而,我不知道如何真正对我的域实体进行单元测试,除非我可以模拟它们所依赖的其他实体,这需要松散耦合。

我也不确定想要一个可以交换各个部分的域模型是否现实。

We're finally building a domain model. The domain model includes interfaces for loosely coupling domain objects to persistence. I'm however wondering how coupled the domain model objects should be to each other.

Does Order point to a Customer or to an ICustomer?

This post mentions problems with aggressively de-coupling objects and seems to discourage "going overboard with [interfaces]". I however don't see how I can truly unit test my domain entities unless I can mock the other entities they depend on, which requires loose coupling.

I'm also uncertain about how realistic it is to want a domain model in which pieces could be swapped out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗化ㄋ丶相逢 2024-11-03 13:32:08

当协作者满足以下条件时,我不介意在单元测试中使用具体协作者:

1)拥有一套全面的单元测试,明确指定其行为。

2)安排起来并不困难。

3) 不利用外部资源(或违反任何这些相关准则)。

我们一直使用框架类(例如 DateTimestring)来执行此操作 - 除非聚合的子级异常复杂,否则您也应该能够信任它。

I don't mind using concrete collaborators in unit tests when the collaborator:

1) Has a comprehensive set of unit tests that clearly specifies its behavior.

2) Isn't difficult to arrange.

3) Doesn't utilize external resources (or break any of these related guidelines).

We do this all the time with framework classes (say, DateTime or string) - unless an aggregate's child is unusually complex, you should be able to trust it too.

指尖上的星空 2024-11-03 13:32:08

域模型包括用于将域对象松散耦合到持久性的接口。

持久性持久您的对象。它知道你的对象以便持久保存它们。将持久性与模型分离不会给你带来任何好处。领域中的任何变化都必须反映在持久层中。

在进行 DDD 时,您与领域专家一起确定通用语言,然后用代码表示。我还没有看到领域专家提到接口。通过识别领域中的正确概念来解耦模型中的 AR。您可能为某些域服务定义了接口,但请确保它们是您域中真正的服务提供者,而不是您错过的一些概念。

我也不确定想要一个可以交换各个部分的域模型是否现实。

你说得对,这不现实。您可能会交换某个服务提供商的实现,但是 AR 中的实体呢?我不这么认为。

The domain model includes interfaces for loosely coupling domain objects to persistence.

The persistence persists your objects. It knows your object in order to persist them. Decoupling the persistence from the model does not give you anything. Any change in the domain will have to be reflected in the persistence layer,

When doing DDD you work with the domain expert to identify the ubiquitous language, which you then represent in code. I have yet to see a domain expert mentioning an interface. Decouple the ARs in your model by identifying the right concepts in your domain. You might have interfaces defined for some domain services, but make sure they are real service providers in your domain and not some concepts you missed.

I'm also uncertain about how realistic it is to want a domain model in which pieces could be swapped out.

You are right, it's not realistic. You might swap the implementation for a certain service provider, but entities in an AR? I don't think so.

以为你会在 2024-11-03 13:32:08

在进行 DDD 时,您不想“真正解耦实体”。您应该首先将域模型划分为应该解耦的聚合。在聚合内部,不需要模拟任何东西,因为聚合应该被视为单元测试的“单元”。

此外,将尽可能多的行为移至值对象将帮助您使域模型可测试,因为值对象(根据定义,它们是不可变的)非常容易测试。

When doing DDD you don't want to 'truly decouple entities'. You should start by partitioning your domain model into Aggregates which should be decoupled. Inside Aggregates there is no need to mock anything because an Aggregate should be treated as a 'unit' for unit tests.

Also, moving as much behavior as possible to Value Object will help you make your domain model testable as Value Objects (which are, by definition, immutable) are very easy to test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文