IDisposable ASP.net MVC Controller
I'm creating an EntityFramework
object in the default constructor of my Controller.
To free the memory after calling action method inside any controller, I want to make the controller disposable. Is this a good idea?
public somethingController : Controller , IDisposable
{
// implement the dispose method here
public void Dispose ()
{
EntityFrameWorkObject.Dispose();
}
}
what do you think?开发者_JAVA技巧
I recommend IHttpModule implementation for dispose datacontext object. My actual code working with Microsoft unity.
public void Init(HttpApplication application)
{
application.EndRequest += new EventHandler(this.Application_EndRequest);
}
private void Application_EndRequest(object source, EventArgs e)
{
IoCWorker.Resolve<IRepositoryContext>().Terminate();
}
Yes, that is good idea. Actually it's recommended pattern and is generally used. If you want to have class wide object, and want to free its resources after class is freed, you do it in Dispose()
Yes, that is right.
public somethingController : Controller
{
// implement the dispose method here
public void Dispose ()
{
EntityFrameWorkObject.Dispose();
}
}
You don't need to add IDisposable
because the controller call already implements it.
精彩评论