数据仓储模式UnitOfWorks和Repository的实现(
网上看了相关内容关于UnitOfWorks和Repository的数据仓储模式的实现,也来动手搭建下。
ORM使用微软自己的EF来实现
建立一个项目,使用EF,我采用的是DBFirst。建立好连接,连上数据库拖入我要的表,DBFirst有时候还是挺方便的。
然后就要开始实现这个数据仓储模式了
建立泛型接口IUnitOfWorks
具体实现代码:
1 | public interface IUnitOfWorks<TContext> {bool IsCommit { get; set; }void SetDb(TContext context);IQueryable<T> All<T>() where T : class;IQueryable<T> Where<T>(Expression<Func<T, bool>> whereLambda) where T : class;int Count<T>() where T : class;int Count<T>(Expression<Func<T, bool>> whereLambda) where T : class;int Add<T>(T model) where T : class;int Update<T>(T model) where T : class;int Update<T>(T model, params string[] proName) where T : class;int Delete<T>(T model) where T : class;int Delete<T>(Expression<Func<T, bool>> whereLambda) where T : class;int SaveChanges(bool validatonSave = true);void Dispose(); } |
对数据的各种操作
1 | public interface IRepository<T>where T : class {IQueryable<T> All();IQueryable<T> Where(Expression<Func<T, bool>> whereLambda);int Count();int Count(Expression<Func<T, bool>> whereLambda);int Add(T model, bool IsCommit = false);int Update(T model, bool IsCommit = false);int Update(T model, bool IsCommit=false,params string[] proName);int Delete(T model,bool IsCommit=false);int Delete(Expression<Func<T, bool>> whereLambda,bool IsCommit=false); } |
对于IsCommit是否默认提交,设置成可选参数,默认设置成不直接提交需要最终统一提交以实现事务操作。当然也可以直接设置IsCommit为True那么可以单个操作提交.
看起来这两接口是不是挺像的
接下来是这两接口的具体实现
1 | public class Repository<TContext, T> : IRepository<T>where TContext : DbContextwhere T : class {protected TContext context;protected DbSet<T> dbSet;protected T entity;public Repository(TContext dbcontext) { context = dbcontext; dbSet = dbcontext.Set<T>(); }public IQueryable<T> All() {return dbSet.AsQueryable(); }public IQueryable<T> Where(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) {return dbSet.Where(whereLambda); }public int Count() {return dbSet.Count(); }public int Count(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) {return dbSet.Where(whereLambda).Count(); }public int Add(T model, bool IsCommit=false) { dbSet.Add(model);int i_flag = IsCommit ? context.SaveChanges() : 0;return i_flag; }public int Update(T model, bool IsCommit = false) {var entry = context.Entry<T>(model); entry.State = EntityState.Modified; dbSet.Attach(model);int i_flag = IsCommit ? context.SaveChanges() : 0;return i_flag; }public int Update(T model, bool IsCommit=false,params string[] proName) {var entry = context.Entry<T>(model); entry.State = EntityState.Unchanged;foreach (string s in proName) { entry.Property(s).IsModified = true; }int i_flag = IsCommit ? context.SaveChanges() : 0;return i_flag; } public int Delete(T model,bool IsCommit=false) { //var entry = context.Entry<T>(model); dbSet.Attach(model); dbSet.Remove(model);int i_flag = IsCommit ? context.SaveChanges() : 0;return i_flag; }public int Delete(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda,bool IsCommit=false) {var enties = dbSet.Where(whereLambda).ToList();foreach (var item in enties) { dbSet.Remove(item); }int i_flag = IsCommit ? context.SaveChanges() : 0;return i_flag; } } |
1 | public class UnitOfWorks<TContext> : IUnitOfWorks<TContext>where TContext : DbContext {protected TContext dbContext;protected bool _IsCommit=false;public bool IsCommit { get { return _IsCommit; } set { _IsCommit = value; } }public UnitOfWorks() { dbContext = (TContext)EFContextFactory.GetDbContext(); }public void SetDb(TContext context) { dbContext = context; }private IDictionary<Type, object> RepositoryDic = new Dictionary<Type, object>();protected IRepository<T> GenericRepository<T>() where T : class {return new Repository<DbContext, T>(dbContext); }public IRepository<T> GetRepository<T>()where T : class { IRepository<T> repository = null;var key = typeof(T);if (RepositoryDic.ContainsKey(key)) { repository = (IRepository<T>)RepositoryDic[key]; }else { repository = GenericRepository<T>(); RepositoryDic.Add(key, repository); }return repository; }public IQueryable<T> All<T>() where T : class {return GetRepository<T>().All(); }public IQueryable<T> Where<T>(Expression<Func<T, bool>> whereLambda) where T : class {return GetRepository<T>().Where(whereLambda); }public int Count<T>() where T : class {return GetRepository<T>().Count(); }public int Count<T>(Expression<Func<T, bool>> whereLambda) where T : class {return GetRepository<T>().Count(whereLambda); }public int Add<T>(T model) where T : class {return GetRepository<T>().Add(model, IsCommit); }public int Update<T>(T model) where T : class {return GetRepository<T>().Update(model, IsCommit); }public int Update<T>(T model, params string[] proName) where T : class {return GetRepository<T>().Update(model,IsCommit, proName); }public int Delete<T>(T model) where T : class {return GetRepository<T>().Delete(model, IsCommit); }public int Delete<T>(Expression<Func<T, bool>> whereLambda) where T : class {return GetRepository<T>().Delete(whereLambda, IsCommit); }public int SaveChanges(bool validatonSave = true) {if (!validatonSave) dbContext.Configuration.ValidateOnSaveEnabled = false;return dbContext.SaveChanges(); }public void Dispose() {if (dbContext != null) dbContext.Dispose(); GC.SuppressFinalize(this); } } |
具体使用方法:
使用IOC的方式,属性注入来实现
public IUnitOfWorks
//查询
var query = uow.Where(b=>b.id==”test”);
//新增 对表employee进行新增人员
employee empa = new employee();
employee empb = new employee();
empa.id=”001”;
empa.name=”a”;
empb.id=”002”;
empb.name=”b”;
uow
uow
uow.savechange();//实现统一提交