单元测试纯域驱动类

发布于 2024-10-17 03:10:24 字数 760 浏览 3 评论 0原文

我的目标是创建一个基于纯领域驱动设计的系统。据我所知,这意味着我的域对象应该有行为,但没有形状。也就是说,它们不应该有任何吸气剂或其他访问器。

与此同时,我尝试遵循 TDD 流程,但在我尝试编写的测试中遇到了障碍。

[Test]
public class new_purchase_order_should_have_purchase_ordernumber_of_1
{
     PurchaseOrder po = PurchaseOrder.CreatePurchaseOrder()
     Assert.AreEqual(1,po.PurchaseOrderNumber); 
}

public class PurchaseOrder
{
       private int _purchaseOrderNumber;
       static CreatePurchaseOrder()
       {
           _purchaseOrderNumber = SomeWayOfGettingAPONumber()
           //other initialisation
       }

        public int PurchaseOrderNumber {get { return _purchaseOrderNumber;}
}

如果不允许 getter,我如何验证 CreatePurchaseOrder() 方法是否正确运行并将值设置为 1。

这对我尝试实现此设计来说是一个很大的概念障碍,因此任何建议都非常有用。

谢谢

I am aiming to create a system which is based on pure domain driven design. As far as I am aware this means that my domain objects should have behaviour but not shape. That is, they should not have any getters or other accessors.

At the same time I am trying to follow TDD processes and have come across a stumbling block with a test I am trying to write.

[Test]
public class new_purchase_order_should_have_purchase_ordernumber_of_1
{
     PurchaseOrder po = PurchaseOrder.CreatePurchaseOrder()
     Assert.AreEqual(1,po.PurchaseOrderNumber); 
}

public class PurchaseOrder
{
       private int _purchaseOrderNumber;
       static CreatePurchaseOrder()
       {
           _purchaseOrderNumber = SomeWayOfGettingAPONumber()
           //other initialisation
       }

        public int PurchaseOrderNumber {get { return _purchaseOrderNumber;}
}

If getters are not allowed how do I verify that the CreatePurchaseOrder() methods functions correctly and sets a value of 1.

This is a big conceptual hurdle to me in trying to implement this design so any advice would be really useful.

Thanks

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

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

发布评论

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

评论(2

世界等同你 2024-10-24 03:10:24

为什么域对象不能有属性?你所说的纯粹行为,只是静态方法,它们与域对象无关。

维基百科告诉我们:

业务对象本身通常不执行任何操作,而是保存一组实例变量或属性(也称为属性)以及与其他业务对象的关联,从而编织表示业务关系的对象映射。

业务对象没有行为的域模型称为贫乏域模型。

因此,领域对象应该具有属性和(在大多数情况下)行为。

马丁·福勒 说:

域模型 - 包含行为和数据的域对象模型

Why domain object can't have properties? Pure behavior you talk about, it is just static methods, they have nothing to do with Domain Objects.

The wikipedia tells us:

a business object usually does nothing itself but holds a set of instance variables or properties, also known as attributes, and associations with other business objects, weaving a map of objects representing the business relationships.

A domain model where business objects do not have behavior is called an Anemic Domain Model.

So, it turns, that domain object should have properties and (in most cases) behavior.

Martin Fowler says:

Domain Model - An object model of the domain that incorporates both behavior and data

风筝有风,海豚有海 2024-10-24 03:10:24

实现这一目标的一种方法是遵循 CQRS 想法。

CQRS 将查询与架构级别的行为(因此得名)分开,并使用域模型发布的事件来公开状态。

但要正确把握和实施却相当困难。特别是如果您之前没有一般领域驱动设计思想的经验。

因此 - 不要犹豫公开状态,只需确保它只能从对象本身进行修改即可。

One way to achieve this would be to follow CQRS ideas.

CQRS divides queries from behavior on architectural level (hence the name) and use domain model published events to expose state.

But it's quite hard to grasp and implement properly. Especially if You got no prior experience with domain driven design ideas in general.

Therefore - don't hesitate to expose state, just make sure it can be modified from object itself only.

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