Nhibernate Series II sessionstoragebase
Using nhib.pdf;
Using system. Web;
Using system. collections;
Namespace com. zfrong. Arch. Common. Data. NH. session. Storage
{
Internal abstract class sessionstoragebase: isessionstorage
{
Protected static object locker = new object ();
Protected static isessionstorage instance;
Const string sessionkeyname = "session _";
Const string sessionfactorykeyname = "sessionfactory _";
Protected abstract idictionary items
{
Get;
}
Public bool removesession (string key)
{
Key = string. Format ("{0}, {1}", sessionkeyname, key );
Isession session = (isession) This. items [Key];
If (session! = NULL & (session. isconnected | session. isopen ))
{
Session. Disconnect ();
Session. Close (); Session. Dispose ();
This. Items. Remove (key );
Return true;
}
Return false;
}
Public bool removesessionfactory (string key)
{
Key = string. Format ("{0}, {1}", sessionfactorykeyname, key );
Isessionfactory sessionfactory = (isessionfactory) This. items [Key];
If (sessionfactory! = NULL &&! Sessionfactory. isclosed)
{
Sessionfactory. Close (); sessionfactory. Dispose ();
This. Items. Remove (key );
Return true;
}
Return false;
}
Public isession getsession (string key)
{
Isession session = (isession) This. items [String. Format ("{0}, {1}", sessionkeyname, key)];
If (session! = NULL &&! Session. isconnected)
Session. Reconnect ();
Return session;
}
Public isessionfactory getsessionfactory (string key)
{
Return (isessionfactory) This. items [String. Format ("{0}, {1}", sessionfactorykeyname, key)];
}
Public void setsession (string key, isession value)
{
Key = string. Format ("{0}, {1}", sessionkeyname, key );
If (value! = NULL)
{
If (this. Items. Contains (key ))
{
This. items [Key] = value;
}
Else
{
This. Items. Add (Key, value );
}
}
Else
{
This. Items. Remove (key );
}
}
Public void setsessionfactory (string key, isessionfactory value)
{
Key = string. Format ("{0}, {1}", sessionfactorykeyname, key );
If (value! = NULL)
{
If (this. Items. Contains (key ))
{
This. items [Key] = value;
}
Else
{
This. Items. Add (Key, value );
}
}
Else
{
This. Items. Remove (key );
}
}
}
}