Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as off topic by maple_shaft Nov 2 '12 at 17:45

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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. –  Euphoric Jul 7 '12 at 18:51
    
"I store sessions in a SessionManager" that should be a SessionStorage then :] codinghorror.com/blog/2006/03/… –  stijn Jul 8 '12 at 8:10
    
@Euphoric thanks for the additional input. –  lurkerbelow Jul 8 '12 at 14:31
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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? –  lurkerbelow Jul 7 '12 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. –  Olivier Jacot-Descombes Jul 7 '12 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! –  lurkerbelow Jul 8 '12 at 14:36
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.