-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Don't implement IDisposable on Controller types #3726
Comments
While I haven't looked at the I like how @nblumhardt put it almost six years ago:
Just replace the term interface with abstract base class - it makes no difference. Implementation of |
Here it is: public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases all resources currently used by this <see cref="Controller"/> instance.
/// </summary>
/// <param name="disposing"><c>true</c> if this method is being invoked by the <see cref="Dispose"/> method,
/// otherwise <c>false</c>.</param>
protected virtual void Dispose(bool disposing)
{
} |
I agree with this. There isn't any requirement on controller class definitions any more, so why force the disposable implementation? |
But if you have a resource that you want to dispose when the controller's life ends, such as a database context, wouldn't this be very handy for you to rely on the framework for this? |
@itomek, what you should do in that case is implement So with the current design of MVC, your code will look like this: public sealed class MyController : Controller
{
protected override void Dispose(bool disposing) {
try {
base.Dispose(disposing);
} finally {
// Your dispose code here
}
}
} Do note that, in case your code lacks the call to With the alternative design where public sealed class MyController : Controller, IDisposable
{
public void Dispose() {
// Your dispose code here
}
} Since there is no |
Hmm, so what would call the Dispose method in the alternate design? |
@itomek: The same piece of code that is calling |
ok. thanks. |
The |
We implemented We agree that it would have been fine to not have it implemented there and leave it up to individual controllers to choose to implement In the case of ViewComponents they don't implement |
@Eilon Taking into account how much this new version of ASP.NET already breaks compatibility, I don't find that argument compelling... |
@ploeh we focused a lot on application-level source code compatibility for MVC 5 applications. We've preserved a lot of names and patterns to ensure compatibility. The decision is always made on a case-by-case basis and we have to weigh the costs and benefits. In this particular case the benefit seemed minimal, and the cost seemed higher. |
The
Microsoft.AspNet.Mvc.Controller
type implementsIDisposable
, while it doesn't do any disposing itself. This causes all controller implementations (in case they derive fromController
, which they don't have to do anymore, which is awesome btw) to be registered for disposal, and be disposed at the end of the request, while under normal conditions controllers have nothing to dispose. In the common case, any disposable resource is hidden behind some service that is injected into the controller.In case developers need to dispose resources from within their controller class, they can implement
IDisposable
themselves and the framework can still make sure that controllers are disposed.The text was updated successfully, but these errors were encountered: