I've been working in C# for a while, but I'm just recently starting to force myself to use better abstracted and generic code. I recently encountered a situation where I was juggling a lot of session values on a page, so I made a wrapper class for the session to enable easy and safe get/sets of session values.
I'm not entirely sure if this is the best way to go about accomplishing what I want, but I would love some feedback on ways this code can be improved. I'm aware that it can currently only be used for string values, but I'm just using it right now for storing form values between multiple postbacks in a very complex page, so that's not a huge concern for me at the moment.
Base class
abstract public class SessionManager
{
public List<string> PossibleKeys;
public string GetValue(HttpSessionState pSession, string pKey)
{
if (this.PossibleKeys.Contains(pKey))
return ((SessionManager)pSession[this.SessionKey]).Values[pKey].ToString();
else
return "";
}
public bool SetValue(HttpSessionState pSession, string pKey, string pValue)
{
if (this.PossibleKeys.Contains(pKey))
{
var tParameters = (SessionManager)pSession[this.SessionKey];
tParameters.Values[pKey] = pValue.Trim();
pSession[this.SessionKey] = tParameters;
return true;
}
return false;
}
abstract class Keys { }
private Dictionary<string, string> Values { get; set; }
private string SessionKey { get; set; }
private static string DefaultKey = "parameters";
protected void Construct(HttpSessionState pSession, string pSessionKey = null)
{
if (pSessionKey.IsNullOrWhitespace())
this.SessionKey = SessionManager.DefaultKey;
else
this.SessionKey = pSessionKey.Trim();
this.Values = new Dictionary<string, string>();
foreach (var tKey in this.PossibleKeys)
{
this.Values.Add(tKey, "");
}
SessionManager.Save(pSession, this, this.SessionKey);
}
private static void Save(HttpSessionState pSession, SessionManager pSelf, string pSessionKey)
{
pSession[pSessionKey] = pSelf;
}
}
Implemented class on the page codebehind
public class DataManager : SessionManager
{
public class Keys
{
public static string MemberID = "MemberID";
public static string ProviderID = "ProviderID";
public static string ReferredToID = "ReferredToID";
public static string SpecialtyID = "SpecialtyID";
public static string DiagnosisID = "DiagnosisID";
public static string CPTServiceID = "CPTServiceID";
public static string FromDate = "FromDate";
public static string ToDate = "ToDate";
}
public DataManager(HttpSessionState pSession, string pSessionKey = null)
{
this.PossibleKeys = new List<string>()
{
DataManager.Keys.MemberID
, DataManager.Keys.ProviderID
, DataManager.Keys.ReferredToID
, DataManager.Keys.SpecialtyID
, DataManager.Keys.DiagnosisID
, DataManager.Keys.CPTServiceID
, DataManager.Keys.FromDate
, DataManager.Keys.ToDate
};
base.Construct(pSession, pSessionKey);
}
}
Actual use in the page codebehind
public DataManager Parameters { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Parameters = new DataManager(this.Session);
}
this.Parameters.SetValue(this.Session, DataManager.Keys.DiagnosisID, "Blah blah, your value goes here");
string test = this.Parameters.GetValue(this.Session, DataManager.Keys.DiagnosisID);
}