Friday 21 October 2016

c# - Implement IDisposable in abstract class




I have following abstract class and need to implement IDisposable to make sure that DbContent will be disposed all the time properly.



public abstract class DataRepositoryBase : DataRepositoryBase

where T : class, IIdentifiableEntity, new()
{

}


what is the best way to achieve that?



Please NOTE: I'm greatly aware on how to use IDisposable in concrete classes but implementation of this functionality in the abstract class - not very clear


Answer




I would take the implementation provided in this answer:



Proper use of the IDisposable interface



And in derived types override the Dispose(bool disposing) method if the derived type needs to also dispose of stuff, remembering to call base. If it doesn't need to, then simply do nothing with the base implementation.



Make sure it makes sense to expose the need to call Dispose on this type's public contract, it could be a sign of a leaked abstraction (though not always).



This would at the very least be a good start.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...