- DataClasses1DataContext db=new LINQ.DataClasses1DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["XXB"].ToString())
这样,在开发环境与生成环境只要配置一个CONFIG文件即可。灵活。
而今天的主题是线程共享的DbContext与私有的DbContext,所以开始书归正转了,对于ado.net架构中,我们往往使用一个static全局对象来完成数据访问工作,而在linq to sql中,如果你建立一个static对象,它会出现很多问题,这在实现开发过程中才可以体会到,所以,今天要说的不是static对象。
一 线程共享的DbContext,说清楚一点就是在一个线程内,你的DataContext对象是共享的,是一个对象,不是new出很多个datacontext对象来,这事实上是一种单例模式的体现,这没有问题,它解决了static对象所产生的问题,而又满足了多表关联查询时出现(不能实现不同数据上下文件的引用,linq to sql和Ef都是这样的)的问题。
代码:
datacontext生成工厂:
- /// <summary>
- /// 数据库建立工厂
- /// Created By : 张占岭
- /// Created Date:2011-10-14
- /// Modify By:
- /// Modify Date:
- /// Modify Reason:
- /// </summary>
- internal sealed class DbFactory
- {
- #region Fields
- static System.Timers.Timer sysTimer = new System.Timers.Timer(10000);
- volatile static Dictionary<Thread, DbContext[]> divDataContext = new Dictionary<Thread, DbContext[]>();
- #endregion
- #region Constructors
- /// <summary>
- /// 类构造方法
- /// </summary>
- static DbFactory()
- {
- sysTimer.AutoReset = true;
- sysTimer.Enabled = true;
- sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
- sysTimer.Start();
- }
- #endregion
- #region Static Methods
- /// <summary>
- /// 订阅Elapsed事件的方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- List<Thread> list = divDataContext.Keys.Where(item => item.ThreadState == ThreadState.Stopped).ToList();
- for (int index = 0; index < list.Count; index++)
- {
- for (int refer = 0; refer < divDataContext[list[index]].Length; refer++)
- if (divDataContext[list[index]][refer] != null)
- {
- divDataContext[list[index]][refer].Dispose();
- divDataContext[list[index]][refer] = null;
- }
- divDataContext.Remove(list[index]);
- list[index] = null;
- }
- }
- /// <summary>
- /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
- /// </summary>
- /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
- /// <returns>LINQ数据库连接对象</returns>
- public static DbContext Intance(string dbName)
- {
- return Intance(dbName, Thread.CurrentThread, 1, 0);
- }
- /// <summary>
- /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
- /// </summary>
- /// <param name="dbName"></param>
- /// <param name="dbCount"></param>
- /// <param name="dbIndex"></param>
- /// <returns></returns>
- public static DbContext Intance(string dbName, int dbCount, int dbIndex)
- {
- return Intance(dbName, Thread.CurrentThread, dbCount, dbIndex);
- }
- /// <summary>
- /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
- /// </summary>
- /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
- /// <param name="thread">当前线程引用的对象</param>
- /// <param name="dbCount">linq to sql数据库数量</param>
- /// <param name="dbIndex">当前索引</param>
- /// <returns>LINQ对象上下文</returns>
- public static DbContext Intance(string dbName, Thread thread, int dbCount, int dbIndex)
- {
- if (!divDataContext.Keys.Contains(thread))
- {
- divDataContext.Add(thread, new DbContext[dbCount]);
- }
- if (divDataContext[thread][dbIndex] == null)
- {
- divDataContext[thread][dbIndex] = new DbContext(dbName);
- }
- return divDataContext[thread][dbIndex];
- }
- /// <summary>
- /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
- /// </summary>
- /// <param name="dbName"></param>
- /// <param name="thread"></param>
- /// <returns></returns>
- public static DbContext Intance(string dbName, Thread thread)
- {
- return Intance(dbName, thread, 1, 0);
- }
- #endregion
- }
具