开发者

Is this a good way to manage Disposable objects in ASP.NET MVC?

开发者 https://www.devze.com 2023-01-22 15:13 出处:网络
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 disp

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.

Fo开发者_如何学运维r 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.


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)


+1 and thanks for this question. I can't give you an answer based on my own experience, but I did find this post by Stephen Walter describing exactly your situation (I believe), and a comment by @Craig Stuntz suggesting the same pattern you describe. Sounds like you are good here :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号