数据仓储模式UnitOfWorks和Repository的实现(
网上看了相关内容关于UnitOfWorks和Repository的数据仓储模式的实现,也来动手搭建下。
ORM使用微软自己的EF来实现
建立一个项目,使用EF,我采用的是DBFirst。建立好连接,连上数据库拖入我要的表,DBFirst有时候还是挺方便的。
然后就要开始实现这个数据仓储模式了
建立泛型接口IUnitOfWorks
具体实现代码:
public interface IUnitOfWorks
{
bool IsCommit { get; set; }//是否自动提交
///
/// 设置DbContext上下文
///
///
void SetDb(TContext context);
///
/// 获取所有实体对象
///
///
///
IQueryable
///
/// 根据Lamda表达式来查询实体对象
///
///
///
///
IQueryable
///
/// 获取所有实体数量
///
///
int Count
///
/// 根据表达式获取实体数量
///
///
///
int Count
///
/// 实体对象新增
///
///
///
int Add
///
/// 实体对象修改
///
///
///
int Update
///
/// 实体对象根据字段修改
///
///
///
///
int Update
///
/// 实体对象删除
///
///
///
int Delete
///
/// 删除复核条件的多个实体对象
///
///
///
int Delete
///
/// 修改信息提交
///
///
int SaveChanges(bool validatOnSave= true);
void Dispose();
}
对数据的各种操作
public interface IRepository
where T : class
{
///
/// 获取所有实体对象
///
///
IQueryable
///
/// 根据Lamda表达式来查询实体对象
///
///
///
IQueryable
///
/// 获取所有实体数量
///
///
int Count();
///
/// 根据表达式获取实体数量
///
///
///
int Count(Expression
///
/// 实体对象新增
///
///
///
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
}
对于IsCommit是否默认提交,设置成可选参数,默认设置成不直接提交需要最终统一提交以实现事务操作。当然也可以直接设置IsCommit为True那么可以单个操作提交.
看起来这两接口是不是挺像的
接下来是这两接口的具体实现
public class Repository
where TContext : DbContext
where T : class
{
protected TContext context;
protected DbSet
//protected bool IsCommit;//是否自动提交
protected T entity;
public Repository(TContext dbcontext)
{
cOntext= dbcontext;
dbSet = dbcontext.Set
}
///
/// 获取所有实体对象
///
///
public IQueryable
{
//context.Set
return dbSet.AsQueryable();
}
///
/// 根据Lamda表达式来查询实体对象
///
///
///
public IQueryable
{
return dbSet.Where(whereLambda);
}
///
/// 获取所有实体数量
///
///
public int Count()
{
return dbSet.Count();
}
///
/// 根据表达式获取实体数量
///
///
///
public int Count(System.Linq.Expressions.Expression
{
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
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
entry.State = EntityState.Unchanged;
foreach (string s in proName)
{
entry.Property(s).IsModified = true;
}
int i_flag = IsCommit ? context.SaveChanges() : 0;
return i_flag;
}
///
/// 实体对象删除
///
///
///
{
//var entry = context.Entry
//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
{
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
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
//注册Respository
//public void Register
//{
// var key = typeof(T);
// if (RepositoryDic.ContainsKey(key))
// {
// RepositoryDic.Add(key, respository);
// }
//}
protected IRepository
{
return new Repository
}
public IRepository
where T : class
{
IRepository
var key = typeof(T);
if (RepositoryDic.ContainsKey(key))
{
repository = (IRepository
}
else
{
repository = GenericRepository
RepositoryDic.Add(key, repository);
}
return repository;
}
///
/// 获取所有实体对象
///
///
///
public IQueryable
{
return GetRepository
}
///
/// 根据Lamda表达式来查询实体对象
///
///
///
///
public IQueryable
{
return GetRepository
}
///
/// 获取所有实体数量
///
///
public int Count
{
return GetRepository
}
///
/// 根据表达式获取实体数量
///
///
///
public int Count
{
return GetRepository
}
///
/// 实体对象新增
///
///
///
public int Add
{
return GetRepository
}
///
/// 实体对象修改
///
///
///
public int Update
{
return GetRepository
}
///
/// 实体对象根据字段修改
///
///
///
///
public int Update
{
return GetRepository
}
///
/// 实体对象删除
///
///
///
public int Delete
{
return GetRepository
}
///
/// 删除复核条件的多个实体对象
///
///
///
public int Delete
{
return GetRepository
}
///
/// 修改信息提交
///
///
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();//实现统一提交