DDD - 将ConfigurationService注入实体

时间:2013-07-25 07:17:58

标签: c# .net domain-driven-design

在我的域模型中,我有一个名为“库存”的实体。要在库存中进行某项移动,我需要访问业务级别配置以进行检查。

我在清单实体中有以下方法

public class Inventory
{
  // Some codes, properties and arguments are omitted for brevity.
  public int InventoryId { get; set; }
  public virtual Product Product { get; set; }
  public virtual IList<InventoryTransaction> Transactions { get; set; }
  public virtual IList<Stock> Stocks {get; set; }
  // ........

  public void purchase(double qty, decimal cost) { //....... }
  public double QuantitiesOnHand() { //..... }
  public decimal CostOfItemsOnHand() { //....... }

  // This method require to access certain configuration in order to 
  // process the sale of item.
  public decimal Sell(double qty, decimal cost) { //..... }
}

要处理销售,我需要访问某些配置。注入配置界面以处理此实体内的销售是一种好习惯。它会损害DDD的纯度吗?或者我应该只将这个'Sell()'方法移动到域服务层?

编辑:

public virtual IList<Stock> Stocks {get; set; }已添加到上述类定义中,该定义包含特定库存项目的库存。

1 个答案:

答案 0 :(得分:3)

销售/购买操作看起来不属于这个实体。我的意思是,那些操作可能(并且可能是)只是减量/增量数量。此外,他们的责任可能跨越多个实体。

这些方法适用于某种域名服务。 E.g:

public class InventoryTradeService
{
  public void purchase(Inventory inventory, double quantity)
  public void sell(Inventory inventory, double quantity)
}

关于您的“ConfigurationService”,如果它是您所说的“业务级别配置”,它应该是域模型的一部分。因此,没有理由注入它 - 只需访问它。如果它不是模型的一部分,请考虑合并它。

相关问题