NancyFx/Nancy — pull request #700 by csainty (2012-08-12 06:54:50+00:00)
Title: Sessions

Time to make a PR out of this stuff and collaborate on cleaning up the last bits of it. I don't believe it is production ready just yet, but it is close and GH seems the easiest place to discuss it.

There is now a single ISessionStore interface that all session mechanisms will be based on.

There is a CookieBasedSessionStore which should be backwards compatible with the existing version, same cookie name and encryption/hash format.

Then there is the AbstractIdBasedSessionStore which handles creating, storing and reading a session id in a cookie and lets the implementer handle persistence using that id.
Can optionally handle the id creation process as well, default it just a Guid.
There is a single concrete implementation of this class based on ConcurrentDictionary called InProcessSessionStore.
I expect Sql,Raven,Redis,Mongo etc could all use this abstract class as their starting point and focus on the persistence rather than the HTTP/Nancy interactions.
Simple enough to just start from ISessionStore if you do need more control though :)

It will default to the CookieBased store at the moment, to change implementations you just hook into the container in your bootstrapper and create a new ISessionStore registration with your desired config / lifetime.
Most implementations are going to need more than just a new() level of config and have varying lifetime requirements, so seemed best to either default to CookieBased or a null based like is currently done, rather than try pick one with the scanner.

**Issues to resolve**

1. I don't like the class DynamicSession.cs Its goal is to add basic change tracking to the DynamicDictionary, but the way it is implemented is pretty awful.
My preference would probably be to expose a delegate or event on the DynamicDictionary to receive update notifications, then the session code can wire it up when it creates the instance it needs. This involves more complex changes to the existing class though, so want an opinion before I go hacking into it too far.

2. Serialization. I need to take a closer look at the serialization of the DynamicDictionary. I have at least one little work around in place for it, but I need to see if it is a more general problem with a more general fix. The problem is that the serializer does not convert the DynamicDictionaryItem into it's value before serialization.

3. Docs. Will need to update wiki, and some more xml docs are probably needed as well.