而且最主要的特點(diǎn)就是:每個(gè)業(yè)務(wù)類包含了很多的業(yè)務(wù)驗(yàn)證,狀態(tài)跟蹤等。職責(zé)很單一,便于維護(hù)和理解。
示例代碼如下:
代碼
public class Order
{
private Guid _id;
public Guid Id
{
get { return _id; }
set { _id = value; }
}
public float ShippingCost()
{
return ShippingMethod.ShippingCostTo(this.DispatchAddress, this.ItemsTotalWeight());
}
public float Total()
{
return DiscountOffer.TotalPriceWithDiscountOfferAppliedTo(
this.Items, ShippingCost());
}
public void Process()
{
if (this.CanProcess())
{
// Charge the card
Customer.Card.Charge(this.Total());
// Set the status of the order
this.Status = Status.Shipped;
// Adjust the stock levels
foreach (OrderItem item in Items)
{
item.Product.DecreaseStockBy(item.QtyOrdered);
}
else
{
throw new InvalidOrderStateForOperationException(
String.Format(
"Order {0} cannot be processed in its current state {1}",
this.Id, this.Status.ToString());
}
}
public bool CanProcess()
{
if (!this.Status == Status.Shipped &&!this.Status = Status.Cancelled)
{
return (this.HasEnoughStockFor(me.Items) &&
GetBrokenRules.Count() == 0);
}
else
{
return false;
}
}
public List GetBrokenRules()
{
List brokenRules = new List();
if (Customer == null)
brokenRules.Add(new BrokenBusinessRule()
{
Property = "Customer",
Rule = "An Order must have a Customer"
});
else if (Customer.GetBrokenRules().Count >0)
{
AddToBrokenRulesList(brokenRules, Customer.GetBrokenRules());
}
if (DispatchAddress == null)
brokenRules.Add(new BrokenBusinessRule()
{
Property = "DispatchAddress",
Rule = "An Order must have a Dispatch Address"
});
else if (DispatchAddress.GetBrokenRules().Count >0)
{
AddToBrokenRulesList(brokenRules,
DispatchAddress.GetBrokenRules());
}
// ......
return brokenRules;
}
}
上面的代碼只是Order業(yè)務(wù)類的一部分代碼,但是從代碼中可以看出,這個(gè)類中包含了很豐富的業(yè)務(wù)邏輯。例如,在Process方法中,處理了下面的流程:
1.調(diào)用CanProcess 方法來進(jìn)行下面的驗(yàn)證:
a.Order的是否處于合適的可以被處理的狀態(tài)
b.在Order中訂購的物品是否有足夠的庫存
2.customer用戶給這個(gè)order付款。至于怎么付款,這個(gè)邏輯就包含在了card類中。
3.然后,對(duì)產(chǎn)品的庫存進(jìn)行更新。
可以看出,采用Domain Model方式很適合來來組織復(fù)雜的業(yè)務(wù)邏輯,而且代碼也很容易閱讀和理解(如果在加上重構(gòu))。
3.總結(jié)
通過上面的一些分析和解釋,不知道大家是否現(xiàn)在已經(jīng)清楚:之前提出的問題如何解決。
一個(gè)建議就是:不要太形式化,根據(jù)項(xiàng)目的實(shí)際情況來。這句話可以使相當(dāng)于廢話,但是很多的情況確實(shí)是這樣的,DDD不是萬能的,Transaction Script和Active Record也有它們的優(yōu)勢(shì),合適就好。
謝謝各位!
|