1、定义一个增删改查基础类的接口
public partial interface IRepositoryBase<T> where T : BaseModel
{
T GetById(Guid id);
T GetById(int id);
T GetById(long id);
T GetById(string id);
T GetFirstAsNoTracking(Expression<Func<T, bool>> predicate);
List<T> Get(
Expression<Func<T, Boolean>> predicate);
List<T> GetAsNoTracking(Expression<Func<T, bool>> predicate);
int Insert(T entity);
int Update(T entity);
int Delete(T entity);
bool Exists(Guid ID);
bool Exists(int ID);
bool Exists(long ID);
bool Exists(string ID);
int Count(Expression<Func<T, bool>> predicate);
T SingleOrDefault(Expression<Func<T, bool>> predicate);
T SingleOrDefaultAsNoTracking(Expression<Func<T, bool>> predicate);
DbContext GetSession();
}
2、基础实现类
public class EfRepository<T> : IRepositoryBase<T> where T : BaseModel { private IEntityPool entityBase; public EfRepository(IEntityPool EntityService) { if (EntityService == null) { throw new ArgumentNullException("context"); } entityBase = EntityService; } public void Close() { if (entityBase == null) { throw new ArgumentNullException("context"); } entityBase.CloseSession(); } public T GetById(Guid id) { var db = entityBase.GetSession(); T t = db.Set<T>().Find(id); return t; } public T GetById(int id) { var db = entityBase.GetSession(); T t = db.Set<T>().Find(id); return t; } public T GetById(long id) { var db = entityBase.GetSession(); T t = db.Set<T>().Find(id); return t; } public T GetById(string id) { var db = entityBase.GetSession(); T t = db.Set<T>().Find(id); return t; } public T GetFirstAsNoTracking(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); T t = db.Set<T>().AsNoTracking().FirstOrDefault(predicate); return t; } public int Insert(T entity) { var db = entityBase.GetSession(); db.Set<T>().Add(entity); int flg = db.SaveChanges(); return flg; } public int Update(T entity) { var db = entityBase.GetSession(); int flg = -1; if (db.Entry<T>(entity).State == EntityState.Modified) { } else if (db.Entry<T>(entity).State == EntityState.Detached) { try { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Modified; } catch (InvalidOperationException) { T old = GetById(entity.GetHashCode()); db.Entry(old).CurrentValues.SetValues(entity); } } flg = db.SaveChanges(); return flg; } public int Delete(T entity) { var db = entityBase.GetSession(); db.Entry<T>(entity).State = EntityState.Deleted; return db.SaveChanges(); } public bool Exists(Guid ID) { var db = entityBase.GetSession(); bool flg = db.Set<T>().Find(ID) != null; return flg; } public bool Exists(int ID) { var db = entityBase.GetSession(); bool flg = db.Set<T>().Find(ID) != null; return flg; } public bool Exists(long ID) { var db = entityBase.GetSession(); bool flg = db.Set<T>().Find(ID) != null; return flg; } public bool Exists(string ID) { var db = entityBase.GetSession(); bool flg = db.Set<T>().Find(ID) != null; return flg; } public int Count(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); int count = db.Set<T>().Where(predicate).Count(); return count; } public List<T> Get(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); List<T> list = db.Set<T>().Where(predicate).ToList(); return list; } public List<T> GetAsNoTracking(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); List<T> list = db.Set<T>().AsNoTracking().Where(predicate).ToList(); return list; } public T SingleOrDefault(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); T t = db.Set<T>().SingleOrDefault<T>(predicate); return t; } public T SingleOrDefaultAsNoTracking(Expression<Func<T, bool>> predicate) { var db = entityBase.GetSession(); T t = db.Set<T>().AsNoTracking().SingleOrDefault<T>(predicate); return t; } public void ReLoad(DbContext db) { var entitiesToRefresh = db.Set<T>().Local; var objectContext = ((IObjectContextAdapter)db).ObjectContext; objectContext.Refresh(RefreshMode.StoreWins, entitiesToRefresh); } public DbContext GetSession() { return entityBase.GetSession(); } }
3、各种业务类继承基础类并实现自己的特定接口,其中数据库上下文池接口IEntityPool 使用IOC注入
public class ResourcesRepository : EfRepository<Pip_ResourcesModel>, IResourcesRepository { public ResourcesRepository(IEntityPool EntityService) : base(EntityService) { } public List<Pip_ResourcesModel> GetAllResources() { return this.GetSession().Set<Pip_ResourcesModel>().AsNoTracking().OrderBy(model=>model.OrderID).ToList<Pip_ResourcesModel>(); } public List<Pip_ResourcesModel> GetAllDirResources() { var resList = from res in this.GetSession().Set<Pip_ResourcesModel>().AsNoTracking().OrderBy(model => model.OrderID).ToList() where (res.ResourceType == "dir") select res; return resList.ToList(); } public void DeleteResources(int[] ids) { var dbset = this.GetSession(); var resList = from res in dbset.Set<Pip_ResourcesModel>() where ids.Contains(res.ResourceID) select res; dbset.Set<Pip_ResourcesModel>().RemoveRange(resList); dbset.SaveChanges(); } public void DeleteResource(int id) { Pip_ResourcesModel model = this.SingleOrDefaultAsNoTracking(r=>r.ResourceID==id); if(default(Pip_ResourcesModel) != model){ this.Delete(model); } } public Pip_ResourcesModel GetByIDAsNoTracking(int id) { return this.GetFirstAsNoTracking(s => s.ResourceID == id); } }
<object id="ResourcesRepositoryService" type="Com.JingPai.Pip.Repository.Power.ResourcesRepository,Com.JingPai.Pip.Repository"> <constructor-arg name="EntityService" ref="PipMainEntityService"/> </object>
<object id="PipMainEntityService" type="Com.JingPai.Pip.Model.Pool.PipEntityPool, Com.JingPai.Pip.Model" singleton="false" > </object>