- /// <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
- }
而