我的目标是创建一个基于纯领域驱动设计的系统。据我所知,这意味着我的域对象应该有行为但没有形状。也就是说,它们不应该有任何 getter 或其他访问器。
与此同时,我正在尝试遵循 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。
在尝试实现此设计时,这对我来说是一个很大的概念障碍,因此任何建议都会非常有用。
谢谢
最佳答案
为什么域对象不能有属性?您所说的纯行为,它只是静态方法,它们与域对象无关。
wikipedia告诉我们:
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.
因此,领域对象应该具有属性和(在大多数情况下)行为。
Domain Model - An object model of the domain that incorporates both behavior and data
关于c# - 单元测试纯领域驱动类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4985380/