想象一下,我有一種像下面這樣的方法,它使用一些策略來創建新產品,並將它們作爲結果保存在Db中。應該怎樣避免將UnitOfWork實例傳遞給策略?
而且所有事情都是作爲工作單元的一部分發生的。
(我的關心和)問題: 我擔心的是它確定或預防到的UnitOfWork實例作爲參數傳遞戰略和提交根方法的變化? 如何避免將UnitOfWork實例作爲參數傳遞給策略,但仍使它們作爲工作單元的一部分工作?
(我不喜歡哪位,我不希望這樣的事實,有人可以通過調用錯誤戰略.Commit內實現)
public void DoTheJob(CustomerRequest req)
{
var materialsPikcerStrategyFactory = new PickerStrategyFactory();
var productionStrategyFactory = new ProductionStrategyFactory();
var materialsPickerStrategy = materialsPikcerStrategyFactory.GetStrategy(req);
var productionStrategy = productionStrategyFactory.GetStrategy(req);
using (var uow = new UnitOfWorkFactory())
{
var materials = materialsPickerStrategy.PickMaterials(req, uow);
var products = productionStrategy.CreateProductsWith(materials, uow);
uow.Commit();
}
}
puplic abstract class MaterialsPickerStrategy
{
// Picks some material entities from Db and modifies some of its properties before usage
public abstract ICollection<Material> PickMaterials(CustomerRequest req, IUnitOfWorkFactory uow);
}
public abstract class ProductionStrategy
{
// Gets the materials and creates some new instances by ADD'ing them to the repository
public abstract void CreateProductsWith (ICollection<Material> materials, IUnitOfWorkFactory uow);
}
因此,我可以傳遞屬於我使用的UnitOfWork屬性的存儲庫,而不是使用UnitOfWork。像uow.MaterialsRepository – pencilCake
是的,這是主意。 –