单元测试纯域驱动类
我的目标是创建一个基于纯领域驱动设计的系统。据我所知,这意味着我的域对象应该有行为,但没有形状。也就是说,它们不应该有任何吸气剂或其他访问器。
与此同时,我尝试遵循 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么域对象不能有属性?你所说的纯粹行为,只是静态方法,它们与域对象无关。
维基百科告诉我们:
因此,领域对象应该具有属性和(在大多数情况下)行为。
马丁·福勒 说:
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:
So, it turns, that domain object should have properties and (in most cases) behavior.
Martin Fowler says:
实现这一目标的一种方法是遵循 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.