ASP.NET MVC and Idisposable

How can I use IDisposable interface with asp.net mvc.

-3


a source to share


2 answers


This is not such a bad question as some might think. It just isn't right. Consider the following code:

ActionResult UserList()
{
 using (var ctx = new DbCtx())
 {
   return View(ctx.Users)
 }
}

      

Will not work as the ctx is located long before the view actually iterates over the data, resulting in an object being thrown.



The solution is to get rid of the ctx in the Controller. What's not obvious is that most of the dependencies introduced into our controllers will sooner or later have to implement IDisposable.

This is not a problem with mvc.

0


a source


IDisposable in asp.net mvc is the same as IDisposable elsewhere in the .Net framework.



+1


a source







All Articles