Dispose methods for controllers

Apr 24, 2011 at 11:02 PM

In version 3.0 beta of the tutorial we add the StoreManagerController using Visual Studio to create the CRUD methods.

As part of this Dispose is created, however there's no mention of this within the tutorial, and would assume it would mentioned if it were a good practice.

Google is returning conflicting information, so ... should Dispose be implemented on all of the controllers, or not?

Coordinator
Apr 29, 2011 at 6:46 PM

The word I've gotten is that adding Dispose is a best practice since it will free up your connection as soon as possible, although I've seen conflicting information about how important that is. The controller that's created by the scaffold (StoreManagerController) generates that code, so I left it in, but I didn't think it was necessary to add it to the other controllers.

Getting the balance of best practice vs. simple tutorial is tricky. If this tutorial tried to cover every best practice, it would either be 500+ pages or wouldn't be published yet because I couldn't get everyone to agree on what the best practice is. I'm checking with the EF team to find out if they think this is important enough that it needs to be covered here.

Apr 30, 2011 at 1:37 PM

True enough, but I'd argue that ensuring you're not keeping database connections open is, by far, one of the most important lessons to be taught (I might argue essential, even), as it's one of the biggest things beginners don't realize.

But, I might be bias. I didn't learn about using statements until a number of months into writing code, after reading a slew of tutorials that didn't make a single mention of them. Resource management just isn't mentioned as much as it should be in the majority of other tutorials, in my opinion ...

 

Thanks for the checking with the EF team. I'd be interested in hearing what they say, so I can update my own production code accordingly. (My research before posting the question suggested that the EF is better at managing the connections, and therefore the dispose wasn't necessary - period - in these cases.)