You can use the decorator class, which has a finalizer and a delete method, and warns you about a missed order. Therefore, if your class looks like this:
public class CustomerTracker { public bool IsNew() {...} }
Then define the interface and make it client code:
public interface ICustomerTracker { public bool IsNew(); }
Define a decorator and use it where you create any of these objects:
public class CustomerTrackerMemDecorator : ICustomerTracker { ICustomrTracker tracker; CustomerTrackerMemDecorator (ICustomrTracker tracker) { this.tracker = tracker; } public bool IsNew() { return tracker.IsNew(); } ~CustomerTrackerMemDecorator { Debug.Assert("Missed dispose found!"); } public override Dispose() { tracker.Dispose(); GC.SupressFinalize(this); } }
Then, wherever you are:
CustomerTracker tracker = new CustomerTracker ();
replace it with
ICustomerTracker tracker = new CustomerTrackerMemDecorator (new CustomerTracker ());
source share