数据仓储模式UnitOfWorks和Repository的实现

数据仓储模式UnitOfWorks和Repository的实现(

网上看了相关内容关于UnitOfWorks和Repository的数据仓储模式的实现,也来动手搭建下。

ORM使用微软自己的EF来实现

建立一个项目,使用EF,我采用的是DBFirst。建立好连接,连上数据库拖入我要的表,DBFirst有时候还是挺方便的。

然后就要开始实现这个数据仓储模式了

建立泛型接口IUnitOfWorks和IRepository

具体实现代码:

    public interface IUnitOfWorks
    {
        bool IsCommit { get; set; }//是否自动提交
        /// 
        /// 设置DbContext上下文
        /// 
        /// 
        void SetDb(TContext context);
        /// 
        /// 获取所有实体对象
        /// 
        /// 
        /// 
        IQueryable All() where T : class;

        /// 
        /// 根据Lamda表达式来查询实体对象
        /// 
        /// 
        /// 
        /// 
        IQueryable Where(Expression> whereLambda) where T : class;

        /// 
        /// 获取所有实体数量
        /// 
        /// 
        int Count() where T : class;

        /// 
        /// 根据表达式获取实体数量
        /// 
        /// 
        /// 
        int Count(Expression> whereLambda) where T : class;

        /// 
        /// 实体对象新增
        /// 
        /// 
        /// 
        int Add(T model) where T : class;

        /// 
        /// 实体对象修改
        /// 
        /// 
        /// 
        int Update(T model) where T : class;

        /// 
        /// 实体对象根据字段修改
        /// 
        /// 
        /// 
        /// 
        int Update(T model, params string[] proName) where T : class;

        /// 
        /// 实体对象删除
        /// 
        /// 
        /// 
        int Delete(T model) where T : class;

        /// 
        /// 删除复核条件的多个实体对象
        /// 
        /// 
        /// 
        int Delete(Expression> whereLambda) where T : class;

        /// 
        /// 修改信息提交
        /// 
        /// 
        int SaveChanges(bool validatonSave = true);

        void Dispose();
    }

对数据的各种操作

    public interface IRepository
        where T : class
    {
        /// 
        /// 获取所有实体对象
        /// 
        /// 
        IQueryable All();

        /// 
        /// 根据Lamda表达式来查询实体对象
        /// 
        /// 
        /// 
        IQueryable Where(Expression> whereLambda);

        /// 
        /// 获取所有实体数量
        /// 
        /// 
        int Count();

        /// 
        /// 根据表达式获取实体数量
        /// 
        /// 
        /// 
        int Count(Expression> 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> whereLambda,bool IsCommit=false);
    }

对于IsCommit是否默认提交,设置成可选参数,默认设置成不直接提交需要最终统一提交以实现事务操作。当然也可以直接设置IsCommit为True那么可以单个操作提交.

看起来这两接口是不是挺像的

接下来是这两接口的具体实现


    public class Repository : IRepository
        where TContext : DbContext
        where T : class
    {
        protected TContext context;
        protected DbSet dbSet;
        //protected bool IsCommit;//是否自动提交
        protected T entity;

        public Repository(TContext dbcontext)
        {
            context = dbcontext;
            dbSet = dbcontext.Set();
        }

        /// 
        /// 获取所有实体对象
        /// 
        /// 
        public IQueryable All()
        {
            //context.Set().AsQueryable();
            return dbSet.AsQueryable();
        }

        /// 
        /// 根据Lamda表达式来查询实体对象
        /// 
        /// 
        /// 
        public IQueryable Where(System.Linq.Expressions.Expression> whereLambda)
        {
            return dbSet.Where(whereLambda);
        }

        /// 
        /// 获取所有实体数量
        /// 
        /// 
        public int Count()
        {
            return dbSet.Count();
        }

        /// 
        /// 根据表达式获取实体数量
        /// 
        /// 
        /// 
        public int Count(System.Linq.Expressions.Expression> 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(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(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;
        }

        /// 
        /// 实体对象删除
        /// 
        /// 
        /// (model);
            //entry.State = EntityState.Deleted;
            dbSet.Attach(model);
            dbSet.Remove(model);
            int i_flag = IsCommit ? context.SaveChanges() : 0;
            return i_flag;
        }

        /// 
        /// 删除复核条件的多个实体对象
        /// 
        /// 
        /// 
        public int Delete(System.Linq.Expressions.Expression> 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;
        }
    }
    public class UnitOfWorks : IUnitOfWorks
        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();
        }

        /// 
        /// 设置DbContext上下文
        /// 
        /// 
        public void SetDb(TContext context)
        {
            dbContext = context;
        }

        private IDictionary RepositoryDic = new Dictionary();

        //注册Respository
        //public void Register(IRepository respository)
        //{
        //    var key = typeof(T);
        //    if (RepositoryDic.ContainsKey(key))
        //    {
        //        RepositoryDic.Add(key, respository);
        //    }
        //}

        protected IRepository GenericRepository() where T : class
        {
            return new Repository(dbContext);
        }

        public IRepository GetRepository()
            where T : class
        {
            IRepository repository = null;
            var key = typeof(T);

            if (RepositoryDic.ContainsKey(key))
            {
                repository = (IRepository)RepositoryDic[key];
            }
            else
            {
                repository = GenericRepository();
                RepositoryDic.Add(key, repository);
            }
            return repository;
        }

        /// 
        /// 获取所有实体对象
        /// 
        /// 
        /// 
        public IQueryable All() where T : class
        {
            return GetRepository().All();
        }

        /// 
        /// 根据Lamda表达式来查询实体对象
        /// 
        /// 
        /// 
        /// 
        public IQueryable Where(Expression> whereLambda) where T : class
        {
            return GetRepository().Where(whereLambda);
        }

        /// 
        /// 获取所有实体数量
        /// 
        /// 
        public int Count() where T : class
        {
            return GetRepository().Count();
        }

        /// 
        /// 根据表达式获取实体数量
        /// 
        /// 
        /// 
        public int Count(Expression> whereLambda) where T : class
        {
            return GetRepository().Count(whereLambda);
        }

        /// 
        /// 实体对象新增
        /// 
        /// 
        /// 
        public int Add(T model) where T : class
        {
            return GetRepository().Add(model, IsCommit);
        }

        /// 
        /// 实体对象修改
        /// 
        /// 
        /// 
        public int Update(T model) where T : class
        {
            return GetRepository().Update(model, IsCommit);
        }

        /// 
        /// 实体对象根据字段修改
        /// 
        /// 
        /// 
        /// 
        public int Update(T model, params string[] proName) where T : class
        {
            return GetRepository().Update(model,IsCommit, proName);
        }

        /// 
        /// 实体对象删除
        /// 
        /// 
        /// 
        public int Delete(T model) where T : class
        {
            return GetRepository().Delete(model, IsCommit);
        }

        /// 
        /// 删除复核条件的多个实体对象
        /// 
        /// 
        /// 
        public int Delete(Expression> whereLambda) where T : class
        {
            return GetRepository().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 uow { get; set; }

//查询

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().add(empa);

uow().add(empb);

uow.savechange();//实现统一提交

你可能感兴趣的:(MVC,Asp.net)