IDisposable ASP.net MVC Controller

I create an EntityFramework object in the default constructor of my controller.

To free memory after calling the action method inside any controller, I want to make the controller available. Is that a good idea?

 public somethingController : Controller , IDisposable { // implement the dispose method here public void Dispose () { EntityFrameWorkObject.Dispose(); } } 

what do you think?

+4
source share
3 answers

I recommend the implementation of IHttpModule to host the datacontext object. My actual code works 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(); } 
+2
source

Yes, that's a good idea. In fact, he recommended the template and is commonly used. If you want to have a cool object and want to free its resources after freeing the class, you do this in Dispose ()

0
source

Yes this is correct.

 public somethingController : Controller { // implement the dispose method here public void Dispose () { EntityFrameWorkObject.Dispose(); } } 

You do not need to add IDisposable because the controller call already implements it.

0
source

Source: https://habr.com/ru/post/1348063/


All Articles