这段时间研究了orm框架EF 写一写研究的历程和心得
先贴上核心代码
public interface ITransaction { bool IsTransaction { get;} void BeginTransaction(); int Commit(); void Rollback(); }
public class BaseRepository : ITransaction, IDisposable { private XFDbContext dbContext; /// <summary> /// 连接字符串名称 /// </summary> public string ConnectionName { get; set; } private bool disposed; public BaseRepository() { this.dbContext = new XFDbContext(); this.IsTransaction = false; } public BaseRepository(string connectionName) { this.ConnectionName = connectionName; this.dbContext = new XFDbContext(ConnectionName); this.IsTransaction = false; } #region 增删改 /// <summary> /// 新增实体对象 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="model"></param> /// <returns></returns> public int Insert<TEntity>(TEntity model) where TEntity : class { return this.ChangeObjectState<TEntity>(model, EntityState.Added); } /// <summary> /// 新增实体对象集合 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="models"></param> /// <returns></returns> public int Insert<TEntity>(IEnumerable<TEntity> models) where TEntity : class { return this.ChangeObjectState<TEntity>(models, EntityState.Added); } /// <summary> /// 持久化对象更改 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="model"></param> /// <returns></returns> public int Update<TEntity>(TEntity model) where TEntity : class { return this.ChangeObjectState<TEntity>(model, EntityState.Modified); } /// <summary> /// 更新对象集合 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="models"></param> /// <returns></returns> public int Update<TEntity>(IEnumerable<TEntity> models) where TEntity : class { return this.ChangeObjectState<TEntity>(models, EntityState.Modified); } /// <summary> /// 更新对象部分属性 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="predicate"></param> /// <param name="updateAction"></param> /// <returns></returns> public int Update<TEntity>(Expression<Func<TEntity, bool>> predicate, Action<TEntity> updateAction) where TEntity : class { if (predicate == null) throw new ArgumentNullException("predicate"); if (updateAction == null) throw new ArgumentNullException("updateAction"); //dbContext.Configuration.AutoDetectChangesEnabled = true; var _model = dbContext.Set<TEntity>().Where(predicate).ToList(); if (_model == null) return 0; _model.ForEach(p => { updateAction(p); dbContext.Entry<TEntity>(p).State = EntityState.Modified; }); return Save(); } /// <summary> /// 删除实体对象 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="model"></param> /// <returns></returns> public int Delete<TEntity>(TEntity model) where TEntity : class { return this.ChangeObjectState<TEntity>(model, EntityState.Deleted); } /// <summary> /// 删除实体对象集合 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="models"></param> /// <returns></returns> public int Delete<TEntity>(IEnumerable<TEntity> models) where TEntity : class { return this.ChangeObjectState<TEntity>(models, EntityState.Deleted); } /// <summary> /// 删除实体对象集合(符合部分条件的) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="predicate"></param> /// <returns></returns> public int Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class { List<TEntity> _list = null; _list = dbContext.Set<TEntity>().Where(predicate).ToList(); foreach (var item in _list) { dbContext.Entry<TEntity>(item).State = EntityState.Deleted; } return Save(); } #endregion 增删改 #region 查询 public IList<TEntity> Search<TEntity>(Expression<Func<TEntity, bool>> predicate = null) where TEntity : class { if (predicate == null) { return dbContext.Set<TEntity>().ToList(); } else { return dbContext.Set<TEntity>().Where(predicate).ToList(); } } /// <summary> /// 查询单个记录 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="predicate"></param> /// <returns></returns> public TEntity SearchFirstOrDefault<TEntity>(Expression<Func<TEntity, bool>> predicate = null) where TEntity : class { if (predicate == null) { return dbContext.Set<TEntity>().FirstOrDefault(); } else { return dbContext.Set<TEntity>().Where(predicate).FirstOrDefault(); } } /// <summary> /// 查询多笔记录 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="predicate"></param> /// <returns></returns> public IList<TEntity> SearchList<TEntity>(Expression<Func<TEntity, bool>> predicate = null) where TEntity : class { if (predicate == null) { return dbContext.Set<TEntity>().ToList(); } else { return dbContext.Set<TEntity>().Where(predicate).ToList(); } } #endregion #region 私有方法 private int Save() { int effect = 0; if (!this.IsTransaction) { effect = dbContext.SaveChanges(); } return effect; } /// <summary> /// 变更上下文管理器(对象) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="model"></param> /// <param name="state"></param> private int ChangeObjectState<TEntity>(TEntity model, EntityState state) where TEntity : class { //_context.Configuration.ValidateOnSaveEnabled = false; dbContext.Entry<TEntity>(model).State = state; return Save(); } /// <summary> /// 变更上下文管理器(对象集合) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="model"></param> /// <param name="state"></param> private int ChangeObjectState<TEntity>(IEnumerable<TEntity> model, EntityState state) where TEntity : class { if (model == null) return 0; //_context.Configuration.AutoDetectChangesEnabled = false; model.ToList().ForEach(p => dbContext.Entry<TEntity>(p).State = state); return Save(); } #endregion /// <summary> /// 执行带参数的sql语句,返回List /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strsql"></param> /// <param name="paras"></param> /// <returns></returns> public IEnumerable<T> GetList<T>(string strsql, SqlParameter[] paras) { return dbContext.Database.SqlQuery<T>(strsql, paras).ToList(); } /// <summary> /// 执行不带参数的sql语句,返回list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strsql"></param> /// <returns></returns> public IEnumerable<T> GetList<T>(string strsql) { return dbContext.Database.SqlQuery<T>(strsql).ToList(); } /// <summary> /// 执行带参数的sql语句,返回一个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strsql"></param> /// <param name="paras"></param> /// <returns></returns> public T GetOneEntity<T>(string strsql, SqlParameter[] paras) { return dbContext.Database.SqlQuery<T>(strsql, paras).Cast<T>().First(); } /// <summary> /// 执行不带参数的sql语句,返回一个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strsql"></param> /// <returns></returns> public T GetOneEntity<T>(string strsql) { return dbContext.Database.SqlQuery<T>(strsql).Cast<T>().First(); } public int ExecuteSqlCommand(string sql, params SqlParameter[] paras) { if (this.IsTransaction) { if (dbContext.Database.CurrentTransaction == null) { dbContext.Database.BeginTransaction(); } } return dbContext.Database.ExecuteSqlCommand(sql, paras); } /// <summary> /// 获取查询数量 /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <returns></returns> public int GetCount(string sql, SqlParameter[] paras) { return dbContext.Database.SqlQuery(typeof(int), sql, paras).Cast<int>().First(); } public void BeginTransaction() { this.IsTransaction = true; } public int Commit() { int reault = dbContext.SaveChanges(); this.IsTransaction = false; DbContextTransaction transaction = dbContext.Database.CurrentTransaction; if (transaction != null) { transaction.Commit(); transaction.Dispose(); reault += 1; } return reault; } public void Rollback() { this.IsTransaction = false; DbContextTransaction transaction = dbContext.Database.CurrentTransaction; if (transaction != null) { transaction.Rollback(); transaction.Dispose(); } } public bool IsTransaction { get; private set; } public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { this.dbContext.Dispose(); } } this.disposed = true; } }
时间: 2024-10-27 13:54:38