Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created January 11, 2011 22:23
Show Gist options
  • Save andreabalducci/775282 to your computer and use it in GitHub Desktop.
Save andreabalducci/775282 to your computer and use it in GitHub Desktop.
Hybrid Session store for Castle.Facilities.NHibernateIntegration (httpcontext + fallback on callcontext)
using System;
using System.Collections;
using System.Runtime.Remoting.Messaging;
using System.Web;
using Castle.Facilities.NHibernateIntegration.SessionStores;
namespace Lucilla.Framework.Core.Data.NHFacility
{
internal interface ISessionStoreContextAdapter
{
IDictionary GetDictionary(string key);
void StoreDictionary(string key, IDictionary dictionary);
IDictionary GetStatelessSessionDictionary(string key);
void StoreStatelessSessionDictionary(string key, IDictionary dictionary);
}
internal class CallContextSessionStoreAdapter : ISessionStoreContextAdapter
{
public IDictionary GetDictionary(string key)
{
return CallContext.GetData(key) as IDictionary;
}
public void StoreDictionary(string key, IDictionary dictionary)
{
CallContext.SetData(key, dictionary);
}
public IDictionary GetStatelessSessionDictionary(string key)
{
return CallContext.GetData(key) as IDictionary;
}
public void StoreStatelessSessionDictionary(string key, IDictionary dictionary)
{
CallContext.SetData(key, dictionary);
}
}
internal class HttpContextSessionStoreAdapter : ISessionStoreContextAdapter
{
public IDictionary GetDictionary(string key)
{
return HttpContext.Current.Items[key] as IDictionary;
}
public void StoreDictionary(string key, IDictionary dictionary)
{
HttpContext.Current.Items[key] = dictionary;
}
public IDictionary GetStatelessSessionDictionary(string key)
{
return HttpContext.Current.Items[key] as IDictionary;
}
public void StoreStatelessSessionDictionary(string key, IDictionary dictionary)
{
HttpContext.Current.Items[key] = dictionary;
}
}
public class LucillaSessionStore : AbstractDictStackSessionStore
{
private readonly ISessionStoreContextAdapter _httpContext = new HttpContextSessionStoreAdapter();
private readonly ISessionStoreContextAdapter _callContext = new CallContextSessionStoreAdapter();
private ISessionStoreContextAdapter Context
{
get
{
return HttpContext.Current != null ? _httpContext : _callContext;
}
}
protected override IDictionary GetDictionary()
{
return Context.GetDictionary(SlotKey);
}
protected override void StoreDictionary(IDictionary dictionary)
{
Context.StoreDictionary(SlotKey, dictionary);
}
protected override IDictionary GetStatelessSessionDictionary()
{
return Context.GetStatelessSessionDictionary(StatelessSessionSlotKey);
}
protected override void StoreStatelessSessionDictionary(IDictionary dictionary)
{
Context.StoreStatelessSessionDictionary(StatelessSessionSlotKey, dictionary);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment