LINQ for SQL thread affinity Data context

From http://www.cnblogs.com/sunrack/articles/1130866.html , for services we use mainly Thread Context ( the original allows a static context as well ).

Thread context is very usefull for services.

 

 

 

 

 

 

 

 

 

 

 

 

 

{
 
 

 

 

 

 

 

 

 

 

{
TDataContext result = (TDataContext)
SetOptions(result);
}
 
 
 

 

 

 

 

{
TDataContext result = (TDataContext)
SetOptions(result);
}
{
options.LoadWith<

 

context.LoadOptions = options;
}
 

 

 

 

 

 

 

 

{
}
 

 

 

 

 

 

 

 

{
}
 

 

 

 

 

 

 

 

{
key =
context =
{
context = GetDataContext();

 

context = GetDataContext(ConnectionString);
{
threadData =
}
}
}
 

 

 

 

 

 

 

 

 

 

{

 

 

}

 

 

 

 

 

 

 

 

{

 

 

}

 

 

 

 

 

 

 

 

{

 

 

}
 
 
}
///<summary>/// This class provides several static methods for loading DataContext objects /// in a variety of ways. You can load the data context as normal one new instance/// at a time, or you can choose to use one of the scoped factory methods that/// can scope the DataContext to a WebRequest or a Thread context (in a WinForm app/// for example)./// /// Using scoped variants can be more efficient in some scenarios and allows passing/// a DataContext across multiple otherwise unrelated components so that the change/// context can be shared. ///</summary>publicclassDataContextFactory<TDataContext> where TDataContext : DataContext, new()///<summary>/// Creates a new Data Context for a specific DataContext type with a connection string/// /// Provided merely for compleness sake here - same as new YourDataContext()///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="connectionString"></param>///<returns></returns>publicstatic TDataContext GetDataContext(string connectionString)Type t = typeof(TDataContext);Activator.CreateInstance(t, connectionString);return result; ///<summary>/// Creates a new Data Context for a specific DataContext type/// /// Provided merely for compleness sake here - same as new YourDataContext()publicstatic TDataContext GetDataContext()Type t = typeof(TDataContext);Activator.CreateInstance(t);return result; publicstaticvoid SetOptions(TDataContext context)DataLoadOptions options = newDataLoadOptions();Force>(Force => Force.Characters); //add more rows///<summary>/// Creates a Thread Scoped DataContext object that can be reused./// The DataContext is stored in Thread local storage.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<returns></returns>publicstatic TDataContext GetThreadScopedDataContext()return (TDataContext)GetThreadScopedDataContextInternal(null, null);///<summary>/// Creates a Thread Scoped DataContext object that can be reused./// The DataContext is stored in Thread local storage.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<returns></returns>publicstatic TDataContext GetThreadScopedDataContext(string key)return (TDataContext)GetThreadScopedDataContextInternal(key, null);///<summary>/// Creates a Thread Scoped DataContext object that can be reused./// The DataContext is stored in Thread local storage.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<returns></returns>staticobject GetThreadScopedDataContextInternal( string key, string ConnectionString)if (key == null)"__WRSCDC_" + Thread.CurrentContext.ContextID.ToString();LocalDataStoreSlot threadData = Thread.GetNamedDataSlot(key);object context = null;if (threadData != null)Thread.GetData(threadData);if (context == null)if (ConnectionString == null)elseif (context != null)if (threadData == null)Thread.AllocateNamedDataSlot(key);Thread.SetData(threadData, context);return context;///<summary>/// Returns either Web Request scoped DataContext or a Thread scoped/// request object if not running a Web request (ie. HttpContext.Current)/// is not available.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<param name="ConnectionString"></param>///<returns></returns>publicstatic TDataContext GetScopedDataContext(string key, string connectionString)//if (HttpContext.Current != null)// return (TDataContext)GetWebRequestScopedDataContextInternal(typeof(TDataContext), key, connectionString);return (TDataContext)GetThreadScopedDataContextInternal( key, connectionString);///<summary>/// Returns either Web Request scoped DataContext or a Thread scoped/// request object if not running a Web request (ie. HttpContext.Current)/// is not available.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<returns></returns>publicstatic TDataContext GetScopedDataContext(string key)//if (HttpContext.Current != null)// return (TDataContext)GetWebRequestScopedDataContextInternal(typeof(TDataContext), key, null);return (TDataContext)GetThreadScopedDataContextInternal( key, null);///<summary>/// Returns either Web Request scoped DataContext or a Thread scoped/// request object if not running a Web request (ie. HttpContext.Current)/// is not available.///</summary>///<typeparam name="TDataContext"></typeparam>///<param name="key"></param>///<returns></returns>publicstatic TDataContext GetScopedDataContext()//if (HttpContext.Current != null)// return (TDataContext)GetWebRequestScopedDataContextInternal(typeof(TDataContext), null, null);return (TDataContext)GetThreadScopedDataContextInternal( null, null);
Print | posted on Thursday, January 15, 2009 12:01 AM

Feedback

No comments posted yet.
Title  
Name
Email (never displayed)
Url
Comments   
Please add 5 and 8 and type the answer here: