Skip to content

Instantly share code, notes, and snippets.

@dejanvasic85
Last active July 10, 2016 12:07
Show Gist options
  • Save dejanvasic85/59184570ef7a2119c4993a839dceea37 to your computer and use it in GitHub Desktop.
Save dejanvasic85/59184570ef7a2119c4993a839dceea37 to your computer and use it in GitHub Desktop.
Custom Unity lifetime manager
public class SessionLifetimeManager<T> : Microsoft.Practices.Unity.LifetimeManager
{
private string key;
public SessionLifetimeManager()
{
this.key = typeof(T).Name;
}
/// <summary>
/// Retrieve a value from the backing store associated with this Lifetime policy.
/// </summary>
/// <returns>
/// the object desired, or null if no such object is currently stored.
/// </returns>
public override object GetValue()
{
return HttpContext.Current.Session[this.key];
}
/// <summary>
/// Stores the given value into backing store for retrieval later.
/// </summary>
/// <param name="newValue">The object being stored.</param>
public override void SetValue(object newValue)
{
HttpContext.Current.Session[this.key] = newValue;
}
/// <summary>
/// Remove the given object from backing store.
/// </summary>
public override void RemoveValue()
{
}
}
public interface ISession{
string Get(string key);
void Set(string key, string value);
}
public class SimpleSessionStore : ISession{
public string Get(string key){
return HttpContext.Current.Session[key];
}
public void Set(string key, string value){
HttpContext.Current.Session[key] = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment