I am implementing an ITracker interface that looks something like:
public interface ITracker
{
void Track(ITrackerEvent trackerEvent);
}
I initially created an implementation of this interface wrapping Mixpanel.NET. I then created another one that wraps Application Insights. However, the Application Insights one requires Flush() to send the data to the server.
I don't want to pollute the ITracker interface with a Flush() method just because one of the implementations needs one. It would feel like a leaky abstraction.
However, I need to call this method at some point (probably on application shutdown) and don't want to do so every time Track is called.
Is is possible to call a method when the Tracker is garbage collected at the end of the session? Is this even a good approach?
I feel like I'm missing a trick here!
I'd make
ITrackerto use transaction alike pattern because there may be cases when you might want to discard tracker events, rollback or modify them:And then per implementations:
FlushinsideCommitfor Application insights.List<ITrackerEvent>orBlockingCollection<ITrackerEvent>if concurrency involved) insideTrackmethod and then use your current logic to callMixpanel.NETapi insideCommitmethod implementation forMixpanel.NET.Recommendation:
ITrackershould also implementIDisposablesince trackers usually use resources that need to be disposed.