.net - What's the best way to share a dbcontext between multiple services? -


first of all, i'm sorry if has been answered already. wasn't able find solution.

let's want create order along lines , @ same time create entity.

service layer:

public class orderservice {     private dbcontext context;      public orderservice()     {         context = new dbcontext();     }      public void addorder(order order, list<orderline> lines, anotherentity anotherentity)     {         context.orders.add(order);         context.orderlines.addrange(lines);          var anotherservice = new anotherservice();         anotherservice.addanother(anotherentity)          context.savechanges();     } }  public class anotherservice {     private dbcontext context;      public anotherservice()     {         context = new dbcontext();     }      public void addanother(anotherentity entity)     {         // maybe business rules here          context.someotherentities.add(entity);          context.savechanges();     } } 

controller:

var orderservice = new orderservice(); orderservice.add(order, lines, anotherentity); 

the first issue have different context in both services , therefore 2 different transactions. solutions can think of:

  1. pass dbcontext controller through order service next service. expose context presentation layer. , still have 2 transactions due savechanges() method in each service. solve removing savechanges() in addanother if want call independently presentation layer? nothing saved then.

  2. wrap code in addorder using begintransaction(). if addanother calls third service , uses begintransaction()? end multiple nested transactions.

i know repository/uow pattern , tried implementing can't see how solve prooblem.

am overthinking this?

the best way use ioc container , dependency injection pattern in services , whole software architecture.

// simple example of mvc action method uses injected context public iactionresult someaction() {     var s1 = new anotherservice(this.context);     var s2 = new orderservice(this.context);     // call s1 , s2 business logic      this.context.savechanges(); }   public class anotherservice {     private dbcontext context;      public anotherservice(dbcontext dbcontext)     {         context = dbcontext;     }      public void addanother(anotherentity entity)     {         // maybe business rules here          context.someotherentities.add(entity);      } }  public class orderservice {     private dbcontext context;      public orderservice(dbcontext dbcontext)     {         context = dbcontext;     }      public void addorder(order order, list<orderline> lines, anotherentity anotherentity)     {         context.orders.add(order);         context.orderlines.addrange(lines);          var anotherservice = new anotherservice(context);         anotherservice.addanother(anotherentity)     } } 

yes simple example of usage injected dbcontext can investigate uow there

the unitofwork creates layer shared instance of dbcontext on services. services depends on uow , context can commit uow saves data using dbcontext. instance:

// simple example of mvc action method uses injected context public iactionresult someaction() {     using(var uow = new unitofwork())     {         var s1 = new anotherservice(uow.context);         var s2 = new orderservice(uow.context);         // call s1 , s2 business logic         uow.commit();    // commit method implements  this.context.savechanges();  logic.     } } 

and in implementation services have same instance of dbcontext.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -