c# - How does this implementation of IDisposable work? -
i'm build first n-tier web application, , such, i'm educating myself on principles, , looking @ illustration code. came across great illustration of type of application here: https://github.com/marlabsinc/socialgoal however, i'm confused how ef context on info layer disposed. point, i've disposed of context, either by, example, utilizing using block, or overriding dispose method on mvc controllers. in application, however, dispose method never explicitly called, instead mill class instantiates context derived base of operations class implements idisposable:
factory class:
public class databasefactory : disposable, idatabasefactory { private socialgoalentities datacontext; public socialgoalentities get() { homecoming datacontext ?? (datacontext = new socialgoalentities()); } protected override void disposecore() { if (datacontext != null) datacontext.dispose(); } } disposable class:
public class disposable : idisposable { private bool isdisposed; ~disposable() { dispose(false); } public void dispose() { dispose(true); gc.suppressfinalize(this); } private void dispose(bool disposing) { if (!isdisposed && disposing) { disposecore(); } isdisposed = true; } protected virtual void disposecore() { } } with newbie's eyes, looks context never disposed. way dispose(bool) called, finalizer. , since passes false parameter, virtual disposecore() method never called, , since has called context dispose, never will. right in thinking this, or missing key piece of .net knowledge?
this parameterless dispose() method classic implementation of idisposable interface , done correctly on disposable base of operations class:
public void dispose() { dispose(true); gc.suppressfinalize(this); } dispose() called @ end of using-block, subsequently phone call dispose(true) , disposecore() which, presumably, implemented in derived classes clean managed resources.
this not particularly piece of code
first of all, how dispose-pattern should implemented:
public class mydisposablething : idisposable { public mydisposablething() { // constructor } protected virtual void dispose(bool disposing) { if (disposing) { // dispose managed resources } // dispose unmanaged resources } public void dispose() { dispose(true); gc.suppressfinalize(this); } ~mydisposablething() { dispose(false); } } an instance of type can destroyed in 2 ways:
deterministically: dispose() called explicitly or end of using-block garbage collection: instance doomed gc , finalizer called.in first case, want release managed resources - that's why called dispose(). in second, don't because, definition, managed , gc should handle them handling own object. want clean unmanaged resources. why disposing flag exists, along associated if-statement.
the phone call gc.suppressfinalize() optimization - tells garbage collector instance has been cleaned dispose() , finalizer, ~mydisposablething() need not called again.
finally, note dispose(bool disposing) virtual , vitally important. derived classes introduce new resources need handled during disposal can override and, after releasing own resources (managed or unmanaged) can phone call base.dispose(disposing)
public class myderiveddisposablething : mydisposablething { public myderiveddisposablething() { // constructor } protected override void dispose(bool disposing) { if (disposing) { // dispose managed resources } // dispose unmanaged resources // phone call base of operations class base.dispose(disposing); } } contrast code snippets
firstly, disposecore() has no parameters , not called finalizer (because disposing false) derived classes prevented exploiting whole pattern , cleaning unmanaged resources.
secondly, isdisposed private makes exclusively unnecessary , useless on base of operations class since base of operations class pretty primitive.
opinion
do not create base of operations disposable class @ all. implement dispose pattern shown in reply , on msdn
http://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx
it bit of faff best done properly.
c# asp.net .net entity-framework dispose
No comments:
Post a Comment