-1

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?

2
  • Are you sure you want to save when application closes? What happens when HW crashes and application doesn't have time to save? I would recommend saving every time something changes. Commented Jul 7, 2012 at 18:51
  • "I store sessions in a SessionManager" that should be a SessionStorage then :] codinghorror.com/blog/2006/03/… Commented Jul 8, 2012 at 8:10

1 Answer 1

2

It depends on the type of application. In a WinForms application, for example, you could use the Form.Closing event of your main form. There is also an Application.ApplicationExit event, which works independently of any form.

3
  • So.. basically I should attach to an closing/shutdown event e.g. ApplicationExit. If ApplicationExit occours I would tell any shutdown relevant class (maybe abstracted by an interface like IShutdown) that shutdown is now happening. Right? Commented Jul 7, 2012 at 17:04
  • Yes. If your application is a long running one, you could also start a timer and additionally store the settings at predefined intervals. Commented Jul 7, 2012 at 18:06
  • I'm now using Quartz.NET to persist my sessions every n minutes. Thats good enough, I do not need a 100% reliable system. Session loss isn't that bad in the given environment. thanks for your time! Commented Jul 8, 2012 at 14:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.