c# - How to get DbContext in nested methods using SimpleInjector -


i have doubt since i'm new dependency , ioc.

i have domain layer (with business logic) , data layer. not implement repositories, use ef core directly. class library project, use in aspnetcore web api, winforms , inside framework.

the idea use same context inside scope.

the problem i'm not being able same context in nested method execution, i'm sure because did not understand concept completely, can guys give me on that?

example:

public class mytest {     public void testcontainer()     {         var parentcontext = mycontainer.container.getinstance<mycontext>();         testparentandchildcontext(parentcontext);      }      private void testparentandchildcontext(mycontext parentcontext)     {         var childcontext = mycontainer.container.getinstance<mycontext>();         assert.areequal(parentcontext, childcontext);     } }  public class mycontainer {     public static container container     {         { return container ?? (container = registerandverifycontainer()); }     }      private static container registerandverifycontainer()     {         var container = new container();         container.options.defaultscopedlifestyle = new executioncontextscopelifestyle();         container.register<dbcontext, mycontext>(lifestyle.scoped);          container.verify();          return container;     } } 

in simple injector register implementation abstraction. in case registed mycontext dbcontext base type. point on simple injector know need construct mycontext in case asks dbcontext. whole purpose of

program interface, not implementation

in case however, although register mycontext abstraction, request new instance of mycontext directly, instead of requesting through abstraction. causes simple injector mycontext in list of registered abstractions. since there no registration mycontext (there dbcontext though, that's totally different type simple injector concerned), simple injector try add missing registration. succeeds because mycontext concrete , has single resolvable constructor. default, simple injector resolve unregistered concrete types transient.

so mycontext resolved transient when requested directly. can solve changing test following:

public void testcontainer() {     using (mycontainer.container.beginexecutioncontextscope()) {         var parentcontext = mycontainer.container.getinstance<dbcontext>();         testparentandchildcontext(parentcontext);      } }  private void testparentandchildcontext(mycontext parentcontext) {     var childcontext = mycontainer.container.getinstance<dbcontext>();     assert.areequal(parentcontext, childcontext); } 

do note simple injector detects these kinds of mistakes. in case register mycontext dbcontext base type, inject mycontext directly in constructor of type, simple injector throw short circuited dependency error when calling verify().

the reason didn't warned this, because you've called verify() before resolve action (you should typically not call getinstance within application; instead should build object graphs front). when you'd call verify (again) after resolving mycontext see exception popping up:

[testmethod] public void testcontainer() {     var container = mycontainer.container.getinstance<dbcontext>();     var parentcontext = container.getinstance<mycontext>();     var childcontext = container.getinstance<mycontext>();      // call fail     container.verify(); } 

Comments