I have used TPH in Entity Framework to create the Inventory table in my database.
public abstract class Inventory
{
public Guid Id { get; set; }
public DateTime On { get; set; }
}
public class CycleCountInventory : Inventory
{
public string Frequency { get; set; }
// and other properties
}
public class SkuGroupInventory : Inventory
{
public string SkuGroup { get; set; }
// and other properties
}
I have a requirement where items can be added to the inventory. However, the behaviour of the inventory repo should be different based on the type of inventory. I have a service class that holds a reference to the repo as follows:
public class InventoryService : IInventoryService
{
public bool AddItems(Inventory inventory, IList<Guid> itemsGuidList)
{
// the inventory type is only known at this point
IInventoryRepo repo = (inventory is SkuGroupInventory)
? (IInventoryRepo) new SkuGroupInventoryRepo()
: new CycleCountInventoryRepo();
return repo.PerformInventory(itemsGuidList);
}
}
Currently I am loading the different implementations of IInventoryRepo
by manually doing a check. There are a few problems with this approach: 1. I have using Autofac to resolve all dependencies... this approach makes my code hard to change 2. As more types of inventories are added, my code will become hard to manage
Are there any patterns (Abstract Factory / Strategy etc) I can use to delegate resolving the dependencies back to Autofac. I am sure someone would have faced this before. Thanks!
Aucun commentaire:
Enregistrer un commentaire