Created
February 17, 2013 14:58
-
-
Save jarrettmeyer/4971798 to your computer and use it in GitHub Desktop.
How to work with sessions in .NET
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
public abstract class HttpSession : IHttpSession | |
{ | |
protected IKeyValueStore storage; | |
public virtual int UserId | |
{ | |
get { return Get<int>("user_id"); } | |
set { storage["user_id"] = value; } | |
} | |
public virtual void ResetSession() | |
{ | |
UserId = 0; | |
} | |
protected virtual T Get<T>(string key) | |
{ | |
try | |
{ | |
var obj = storage[key]; | |
return (T)obj; | |
} | |
catch (Exception) | |
{ | |
// This should really catch KeyNotFoundException, InvalidCastException, etc., | |
// but I am being lazy for demo purposes. | |
return default(T); | |
} | |
} | |
} |
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
public class HttpSessionKeyValueStore : IKeyValueStore<string, object> | |
{ | |
private readonly HttpSessionStateBase session; | |
public HttpSessionKeyValueStore(HttpSessionStateBase session) | |
{ | |
if (session == null) | |
throw new ArgumentNullException("session"); | |
this.session = session; | |
} | |
public IEnumerable<string> Keys | |
{ | |
get | |
{ | |
foreach (var key in session.Keys) | |
yield return key.ToString(); | |
} | |
} | |
public object this[string key] | |
{ | |
get { return session[key]; } | |
set { session[key] = value; } | |
} | |
} |
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
public interface IKeyValueStore<TKey, TValue> | |
{ | |
IEnumerable<TKey> Keys { get; } | |
TValue this[TKey key] { get; set; } | |
} |
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
public class InMemoryHttpSession : HttpSession | |
{ | |
public InMemoryHttpSession() | |
{ | |
this.storage = new InMemoryKeyValueStore(); | |
} | |
} |
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
public class InMemoryKeyValueStore : IKeyValueStore<string, object> | |
{ | |
private readonly Dictionary<string, object> dictionary = new Dictionary<string, object>(); | |
public IEnumerable<string> Keys | |
{ | |
get { return dictionary.Keys; } | |
} | |
public object this[string key] | |
{ | |
get { return dictionary[key]; } | |
set { dictionary[key] = value; } | |
} | |
} |
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
public class WebHttpSession : HttpSession | |
{ | |
private readonly HttpSessionStateBase session; | |
public WebHttpSession(HttpSessionStateBase session) | |
{ | |
if (session == null) | |
throw new ArgumentNullException("session"); | |
this.session = session; | |
this.storage = new HttpSessionKeyValueStore(session); | |
} | |
public override void ResetSession() | |
{ | |
session.Clear(); | |
base.ResetSession(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment