Monday, December 8, 2008

Session Facade

I have noticed some blogs about Session Facade but all have one thing missing. CleanUp()! This can be useful when you use point in time session variables and have a strict policy to clean these items up.


internal class DocSharingSessionFacade
{
private static DocSharingSessionFacade sessionFacade = null;
private static readonly string _pointInTimeUploadFileKey = "DocSharingUploadFile";

private DocSharingSessionFacade()
{
}

public static DocSharingSessionFacade Instance()
{
if (sessionFacade == null)
{
sessionFacade = new DocSharingSessionFacade();
}

return sessionFacade;
}

#region UploadFile Methods

public string UploadFile
{
get { return HttpContext.Current.Session[_pointInTimeUploadFileKey] as string; }
set {HttpContext.Current.Session[_pointInTimeUploadFileKey] = value; }
}

public void ClearUploadFile()
{
HttpContext.Current.Session.Remove(_pointInTimeUploadFileKey);
}

#endregion
}


BTW - the reason I am using Session instead of ViewState for this type of variable is that we are using MonoRail and NVelocity so do not actually have a ViewState object.