在使用Linq to Sql做为底层ORM时,它为我们提供的数据上下文为DataContext对象,实现上我们通过拖动生成的DBML文件,它们都是继承自 System.Data.Linq.DataContext类型的,所以DataContext就是LINQ数据对象的基类,有时,我们可以通过这种类的多态性来动态创建DB的实例。
在每个DataContext类中,它有几个实例的构造方法,用来让你创建DataContext的实例,如下:
- /// <summary>
- /// 使用默认的连接串创建实现(每拖一次数据库,就会产生一个连接串)
- /// </summary>
- public DataClasses1DataContext() :
- base(global::test.Properties.Settings.Default.EEE114ConnectionString, mappingSource)
- {
- OnCreated();
- }
- /// <summary>
- /// 使用指定的连接串,可能配置在config文件里
- /// </summary>
- /// <param name="connection"></param>
- public DataClasses1DataContext(string connection) :
- base(connection, mappingSource)
- {
- OnCreated();
- }
- /// <summary>
- /// 使用使用了IDbConnection接口的对象创建实例
- /// </summary>
- /// <param name="connection"></param>
- public DataClasses1DataContext(System.Data.IDbConnection connection) :
- base(connection, mappingSource)
- {
- OnCreated();
- }
- /// <summary>
- /// 使用连接串和数据库的映射文件来建立实例,mappingSource可能是一个XML文件
- /// </summary>
- /// <param name="connection"></param>
- /// <param name="mappingSource"></param>
- public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
- base(connection, mappingSource)
- {
- OnCreated();
- }
而我们在实现项目开发中,可能用第二种比较多,即
- 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
- }
具体领域数据对象创建时代码如下:
- /// <summary>
- /// XXB数据库基类
- /// </summary>
- public class XXB_DataBase : DataBase
- {
- private readonly static string _conn;
- static XXB_DataBase()
- {
- if (ConfigurationManager.ConnectionStrings["XXB"] == null)
- throw new Exception("请设置XXB配置字符");
- else
- _conn = ConfigurationManager.ConnectionStrings["XXB"].ToString();
- }
- public XXB_DataBase()
- : base(DbFactory.Intance(_conn, 2, 1))
- { }
- }
二 私有的DbContext,它要求你为每个表都建立一个repository对象,用户对表进行CURD操作,而它们都继承一个database,在 database里有唯一创建datacontext的入口,这样在做多表关联时,使用的是同一个datacontext对象,所以不会出现“不能实现不同数据上下文件的引用”这种问题,但这样方式感觉很不爽,因为你必须把所有多表关联的业务逻辑,写在DAL层,这是很郁闷的,因为一般我们会把它放在BLL层(更有利于业务的组合与重用)。
代码:
具体领域数据基类:
- /// <summary>
- /// XXB数据基类
- /// </summary>
- public abstract class XXBBase : DataBase
- {
- public XXBBase()
- : base(new LINQ.DataClasses1DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["XXB"].ToString()))
- { }
- }
统一数据基类:
- /// <summary>
- /// 标准数据操作基类
- /// </summary>
- public abstract class DataBase : IRepository
- {
- /// <summary>
- /// 数据访问对象(只对子类可见)
- /// </summary>
- protected DataContext DB;
- #region Constructors
- public DataBase(DataContext db)
- : this(() => { return db; })
- { }
- public DataBase(Func<DataContext> func)
- {
- this.DB = func();
- }
- #endregion
- #region DBContext SubmitChanges
- /// <summary>
- /// XXB默认提交【重写时候可能需要写入自定义的类似约束的逻辑】
- /// </summary>
- protected virtual void SubmitChanges()
- {
- ChangeSet cSet = DB.GetChangeSet();
- if (cSet.Inserts.Count > 0
- || cSet.Updates.Count > 0
- || cSet.Deletes.Count > 0)
- {
- try
- {
- DB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
- }
- catch (System.Data.Linq.ChangeConflictException)
- {
- foreach (System.Data.Linq.ObjectChangeConflict occ in DB.ChangeConflicts)
- {
- occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
- occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
- occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
- }
- DB.SubmitChanges();
- }
- }
- }
- #endregion
- #region IRepository 成员
- public virtual void Update<TEntity>(TEntity entity) where TEntity : class
- {
- this.SubmitChanges();
- }
- public virtual void Update<TEntity>(IEnumerable<TEntity> list) where TEntity : class
- {
- list.ToList().ForEach(entity =>
- {
- this.Update<TEntity>(entity);
- });
- }
- public virtual void Insert<TEntity>(TEntity entity) where TEntity : class
- {
- DB.GetTable<TEntity>().InsertOnSubmit(entity);
- this.SubmitChanges();
- }
- public virtual void Insert<TEntity>(IEnumerable<TEntity> list) where TEntity : class
- {
- DB.GetTable<TEntity>().InsertAllOnSubmit<TEntity>(list);
- this.SubmitChanges();
- }
- public virtual TEntity InsertGetIDENTITY<TEntity>(TEntity entity) where TEntity : class
- {
- this.Insert<TEntity>(entity);
- return GetModel<TEntity>(i => i == entity).FirstOrDefault();
- }
- public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
- {
- DB.GetTable<TEntity>().DeleteOnSubmit(entity);
- this.SubmitChanges();
- }
- public virtual void Delete<TEntity>(IEnumerable<TEntity> list) where TEntity : class
- {
- DB.GetTable<TEntity>().DeleteAllOnSubmit<TEntity>(list);
- this.SubmitChanges();
- }
- public virtual IQueryable<TEntity> GetModel<TEntity>() where TEntity : class
- {
- return this.DB.GetTable<TEntity>();
- }
- public virtual IQueryable<TEntity> GetModel<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) where TEntity : class
- {
- return GetModel<TEntity>().Where(predicate);
- }
- public virtual TEntity Find<TEntity>(params object[] keyValues) where TEntity : class
- {
- var mapping = DB.Mapping.GetTable(typeof(TEntity));
- var keys = mapping.RowType.IdentityMembers.Select((m, i) => m.Name + " = @" + i).ToArray();
- TEntity entityTEntity = DB.GetTable<TEntity>().Where(String.Join(" && ", keys), keyValues).FirstOrDefault();
- if (entityTEntity != null)
- DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entityTEntity);
- return entityTEntity;
- }
- #endregion
- }
而用户模块User_InfoRepository在做多表关联时,是这样完成的:
- public class User_InfoRepository : XXBBase
- {
- /// <summary>
- /// 需要把Join的表关系写在这里
- /// </summary>
- /// <returns></returns>
- public IQueryable<User_Info> GetDetailModel()
- {
- var linq = from data1 in base.GetModel<User_Info>()
- join data2 in base.GetModel<User_Profile>() on data1.UserID equals data2.UserID
- select data1;
- return linq;
- }
- }
原文链接:http://www.cnblogs.com/lori/archive/2012/08/23/2653426.html