Skip to content

Instantly share code, notes, and snippets.

@cathode
Created August 5, 2016 16:32
Show Gist options
  • Save cathode/532926e5fff089482a9420b33fe2b4d5 to your computer and use it in GitHub Desktop.
Save cathode/532926e5fff089482a9420b33fe2b4d5 to your computer and use it in GitHub Desktop.
/// <summary>
/// Provides static properties that return context or request-specific values.
/// </summary>
public static class Current
{
/// <summary>
/// Gets the <see cref="IbisDB"/> instance used for the lifetime of the current web request.
/// </summary>
public static IbisDB Context
{
get
{
if (!HttpContext.Current.Items.Contains(typeof(IbisDB).ToString()))
{
var db = new IbisDB();
HttpContext.Current.Items.Add(typeof(IbisDB).ToString(), db);
HttpContext.Current.AddOnRequestCompleted(a =>
{
db.SaveChanges();
//db.Dispose();
});
}
return HttpContext.Current.Items[typeof(IbisDB).ToString()] as IbisDB;
}
}
public static Session Session
{
get
{
return HttpContext.Current.Items[typeof(Session).ToString()] as Session;
}
internal set
{
HttpContext.Current.Items.Add(typeof(Session).ToString(), value);
}
}
public static Identity Identity
{
get
{
return HttpContext.Current.User.Identity as Identity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment