How do YOU use your datacontext ?
Well, this will be a very short post, and a very simple one, really.
I've just started working on my first _real_ app with linq (I know, a shame, isn't it...), and I was looking to manage my datacontext, so that I wouldn't have to create a new database connection for each query... so I came up with this :
public static class DataContextWrapper<T> where T : DataContext
{ public static T Instance
{ get
{ if (HttpContext.Current.Items["request"] == null)
{ string connString = ConfigurationManager.
ConnectionStrings[0].
ConnectionString;
T dc = typeof(T).GetConstructor(
new Type[] { typeof(string) }). Invoke(new object[] { connString }) as T;
HttpContext.Current.Items["request"] = dc;
}
return HttpContext.Current.Items["request"] as T;
}
}
}
And the usage would be something like this :
MyDataContext dc = DataContextWrapper<MyDataContext>.Instance;
Your thoughts ? How do YOU use it ?