Structuremap Disposing of a DataContext

I wanted to be sure that the structure of the structure would delete my DataContext after each request was completed.

Here is my setup

ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>(); SelectConstructor<MyDataContext>(() => new MyDataContext()); 

Will the structmap automatically delete my datacontext or do I need to call Dispose manually?

+7
source share
2 answers

No, it will not delete it automatically unless you use nested containers and delete the container containing the context instance. It is up to the context creator for Dispose it. The creator will usually be part of your code that calls ObjectContext.GetInstance<MyDataContext> , or the root method that makes StructureMap insert a DataContext into one of your objects.

A common practice is to create context on the HttpRequest and place the context at the end of the request.

+4
source

What am I doing:

  For<IUnitOfWork>() .HybridHttpOrThreadLocalScoped() .Use<BpReminders.Data.NH.UnitOfWork>(); For<ISession>() .HybridHttpOrThreadLocalScoped() .Use(o => ((BpReminders.Data.NH.UnitOfWork)o.GetInstance<IUnitOfWork>()).CurrentSession); 

and...

 protected void Application_EndRequest(object sender, EventArgs e) { ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); } 

HybridHttpOrThreadLocalScoped uses an HttpContext when it is available.

StructureMap keeps an eye on everything. Remember to implement IDisposable in your classes.

+9
source

All Articles