Note that there are some explanatory texts on larger screens.

plurals
  1. POShould and can I avoid passing UnitOfWork instance to strategies through methods?
    primarykey
    data
    text
    <p>Imagine that I have one method like below that is using some strategies to create new products and persists them as a result in the Db. </p> <p>And everything happens as a part of a Unit of Work.</p> <p><strong>(My concern and)Question :</strong> My concern is it OK or preventable to pass the UnitOfWork instance as a parameter to strategies and Commit the changes in the root method? How can I avoid passing the UnitOfWork instance as a parameter to strategies but still make them work as a part of unit of work?</p> <p>(I don't like the fact that someone can call .Commit inside strategy implementations by mistake -which I do not want)</p> <pre><code>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&lt;Material&gt; 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&lt;Material&gt; materials, IUnitOfWorkFactory uow); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload