.net - Unit of work pattern -


i'm looking advices unit of work pattern.

is commit on unit of work called multiple times or 1 time , leaving object garbage collection?

is idea inject unit of work play or should pass around in method call when asking objects perform work?

instances of types implement unit of work pattern have single owner needs control lifetime. methods commit, open, close, , dispose strong signals type should controlled explicitly (or placed behind abstraction if appropriate).

for reason better not inject unit of work instance itself, inject type knows how create such unit of work: factory.

the unit of work in case functions context , when other objects need perform operations in same context (to keep operation atomic instance) need pass it. might this:

public class mycommand {     private readonly iunitofworkfactory factory;      public mycommand(iunitofworkfactory factory)     {         this.factory = factory;     }      public void execute()     {         using (var context = this.factory.createnew())         {             this.dosomenicethings(context);              context.commit();         }     } } 

many di frameworks offer possibility of defining context in object , dependencies run. allows inject unit of work , inject same instance in dependencies. useful feature, not in particular scenario, because correctness of code gets dependant on way configure scope of unit of work. makes code implicit, hard follow , easy break. imo such feature useful in scenario's consumer doesn't care dependency. feature therefore useful performance optimizations, implementing caching strategies , such.

is commit on unit of work called multiple times or 1 time , leaving object garbage collection?

whether calling commit multiple times valid scenario depends on how design it. in production applications, run unit of work inside transaction, allows me commit operations database (to database generated keys instance) while keeping business operation atomic.

i hope helps.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -