views:

67

answers:

2

I am overriding the Controller.Dispose(bool) method in my ASP.NET MVC2 Controllers in order to dispose of things as needed, while leaving them alive for as long as possible. This is as opposed to disposing of them prior to returning from the Controller's action method.

My question, in short; does this work as I expect?

From what I'm seeing so far, this seems to provide exactly what I need; all IDisposable objects are getting disposed (The Controller.Dispose() method calls the virtual Controller.Dispose(bool)), but not until they are no longer needed.


EDIT #2:

I appreciate the advice on what I should be doing. I mean that; I up-voted just such an answer, in fact.

For the purposes of this question, though, let's assume that I have evaluated my own needs and have decided that calling an occasional related-entity-property in my View works best for me, and that I have decided that I am not really in need of a Dependency Injection framework just to make sure my objects are disposed of properly.

What I really just need to know for right now is if anyone is aware of lifetime-related issues to disposing of these objects in an overridden Controller.Dispose(bool) method.

+3  A: 

Take a look into inversion of control and dependency injection. The frameworks out there (Structure Map, Ninject, Autofac, Unity etc.) will take care of disposing objects for you depending on the scope you assigned to that object (ex: per request scope will dispose of objects at the end of a request)

Baddie