Created
February 11, 2015 18:21
-
-
Save Kenneth-Posey/0446d7edaf9b5ca78118 to your computer and use it in GitHub Desktop.
ASP.NET Session Value Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.SessionState; | |
| namespace SitefinityWebApp.Utility | |
| { | |
| public partial class ReferralAuthorizationForm : CustomControl.LoggedInUser | |
| { | |
| 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).ToString(); | |
| } | |
| } | |
| public class DataManager : SessionManager | |
| { | |
| public class Keys | |
| { | |
| public static string ReportType = "ReportType"; | |
| public static string ClaimId = "ClaimId"; | |
| } | |
| public DataManager(HttpSessionState pSession) | |
| { | |
| this.PossibleKeys = new List<string>() | |
| { | |
| DataManager.Keys.ReportType | |
| , DataManager.Keys.ClaimId | |
| }; | |
| base.Construct(pSession); | |
| } | |
| } | |
| abstract public class SessionManager | |
| { | |
| public List<string> PossibleKeys; | |
| public string GetValue(HttpSessionState pSession, string pKey) | |
| { | |
| try | |
| { | |
| if (this.PossibleKeys.Contains(pKey)) | |
| return ((SessionManager)pSession[SessionManager.DefaultKey]).Values[pKey].ToString(); | |
| } | |
| catch (Exception e) | |
| { | |
| #warning Needs error logging implementation | |
| } | |
| //Fallthrough if key isn't right | |
| return String.Empty; | |
| } | |
| public bool SetValue(HttpSessionState pSession, string pKey, string pValue) | |
| { | |
| if (this.PossibleKeys.Contains(pKey)) | |
| { | |
| var tParameters = (SessionManager)pSession[SessionManager.DefaultKey]; | |
| tParameters.Values[pKey] = pValue; | |
| pSession[SessionManager.DefaultKey] = tParameters; | |
| return true; | |
| } | |
| return false; | |
| } | |
| abstract class Keys { } | |
| private System.Collections.Hashtable Values { get; set; } | |
| private string SessionKey { get; set; } | |
| private static string DefaultKey = "parameters"; | |
| protected void Construct(HttpSessionState pSession) | |
| { | |
| var tCurrentManager = pSession[SessionManager.DefaultKey]; | |
| if (tCurrentManager == null) | |
| { | |
| this.Values = new System.Collections.Hashtable(); | |
| foreach (var tKey in this.PossibleKeys) | |
| { | |
| this.Values.Add(tKey, String.Empty); | |
| } | |
| } | |
| else | |
| { | |
| var tCurrent = (SessionManager)pSession[SessionManager.DefaultKey]; | |
| this.Values = tCurrent.Values; | |
| } | |
| SessionManager.Save(pSession, this); | |
| } | |
| // Using a static method here means that we can use the object to save itself | |
| // to the session during the construction of the object | |
| private static void Save(HttpSessionState pSession, SessionManager pSelf) | |
| { | |
| pSession[SessionManager.DefaultKey] = pSelf; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment