2
votes

In my domain model I have an Entity called 'Inventory'. To make a certain move in inventory I need to access business level configuration to check.

I have following methods in the Inventory Entity

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) { //..... }
}

To process the sales I need to access certain configuration. Is it good practice to inject a configuration interface in order to process sale within this entity. Will it damage the purity of DDD? Or should I move only this 'Sell()' method to a Domain service layer?

EDIT :

public virtual IList<Stock> Stocks {get; set; } was added to the above class definition which holds the stock for the particular inventory item.

1
why not pass the configuration value to the sell method? - Davin Tryon
DomainService for simplicity or refactor it when it does harm. - Yugang Zhou
@DavinTryon, Your are right. but I have around 3 to 4 settings which I need to pass. Also I have doubt in future it might increase. Please tell your suggestion. - BlueBird
Yes it is acceptable to pass to method, injecting into entity constructor will violate SRP for other use cases and complicate construction. - eulerfx
You can pass any type of domain service to a method on an entity if it is required for the behavior implemented by the method. If you shift this to a domain service or app service all you're doing is moving business logic away from the entity. - eulerfx

1 Answers

3
votes

Sell/purchase operations doesn't look like they belong to this entity. I mean, those operations could be (and probably are) much more then just decrement/increment quantity. Also, their responsibility probably spans multiple entities.

These methods are good candidates for some kind of domain service. E.g.:

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

Regarding your "ConfigurationService", if it is "business level configuration" as you said, it should be part of domain model. So, there is no reason to inject it - just access it. If it is not part of the model, think about incorporating it.