DCSIMG
How do YOU use your datacontext ? - Sig-

Sig-

Code and stuff.

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 ?
פורסם: Dec 31 2007, 08:22 PM by silver83 | with no comments
תגים:
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 6 and 8 and type the answer here:


Enter the numbers above: