How would I go about avoiding the use of the so called magic strings in the following code?
Specifically the: System.Web.HttpContext.Current.Items["DomainDataContext"]
Basically this class implements a DataContext inside the Controller. This class is a part of a Class Library, and won't have direct access to the DataContext class (ie .dbml file)
public abstract class BaseControllerWithDataContext<TDataContext> : BaseController where TDataContext : DataContext, new()
{
public static TDataContext CurrentContext
{
get
{
if (System.Web.HttpContext.Current.Items["DomainDataContext"] == null)
{
TDataContext context = new TDataContext();
System.Web.HttpContext.Current.Items["DomainDataContext"] = context;
}
return (TDataContext) System.Web.HttpContext.Current.Items["DomainDataContext"];
}
}
protected TDataContext DataContext
{
get { return CurrentContext; }
}
protected override void OnActionExecuted(ActionExecutedContext filteContext)
{
if (System.Web.HttpContext.Current.Items["DomainDataContext"] == null)
{
return;
}
var context = (TDataContext) System.Web.HttpContext.Current.Items["DomainDataContext"];
context.Dispose();
}
}
I tried to do it with another generic type parameter, but was having problems with the "cannot use sealed class 'System.String' as type parameter constraint"
So what would be the best way to go about this ?