I store sessions in a SessionManager. The session manager has a dependency to ISessionPersister.
SessionManager
private readonly ISessionPersister sessionPersister; public SessionManager(ISessionPersister sessionPersister) { this.sessionPersister = sessionPersister; }
ISessionPersister
public interface ISessionPersister : IDisposable { void PersistSessions(Dictionary<string, ISession> sessions); } Q: If my application shuts down how / where do I call PersistSessions? Who is responsible?
First Approach: Use Dispose in SessionManager
protected virtual void Dispose(bool disposing) { if (disposing) { if (this.sessionPersister != null && this.sessionMap != null && this.sessionMap.Count > 0) { this.sessionPersister.PersistSessions(this.sessionMap); } } } Is that the way to go or are there any better solutions?