I am not sure exactly what you are trying to achieve but this might be of interest to people anyway.
I created this class to store objects in session via a key so that you can work on an object over mutliple page requests without having to persist the changes.
Code:
public static class SessionContext<T> where T : class
{
static SessionContext()
{
}
private static Dictionary<Guid, T> fakeSession = new Dictionary<Guid,T>();
public static T GetItem(Guid key)
{
T current = null;
if (HttpContext.Current == null)
{
current = fakeSession[key];
}
else
{
current = (T)HttpContext.Current.Session[key.ToString()];
}
return current;
}
public static void SetItem(T item, Guid key)
{
if (HttpContext.Current == null)
{
fakeSession.Remove(key);
fakeSession.Add(key, item);
}
else
{
HttpContext.Current.Session[key.ToString()] = item;
}
}
}
So you can call:
Code:
SessionContext<MyClass>.SetItem(theObject, key)
and on subsequent requests (I pass the key around in the query string)
Code:
SessionContext<MyClass>.GetItem(key)
You need to make sure that the object is fully loaded before doing this (or at least don't try an access lazy loading properties) and also evict it from the NHibernate session.
Then when you are ready to save it just save it as normal.
I haven't used this with any sort of concurrency considerations, I imagine there will be some issues to resolve there..
Oh, for the record I am pretty anti session, but it does make this scenario quite easy.