分享自http://www.cnblogs.com/lori/p/3478913.html
接口是一组行为规范,看一个简单仓储接口
/// <summary> /// 基础的数据操作规范 /// </summary> /// <typeparam name="TEntity"></typeparam> public interface IRepository<TEntity> where TEntity : class { /// <summary> /// 添加实体并提交到数据服务器 /// </summary> /// <param name="item">Item to add to repository</param> void Insert(TEntity item); /// <summary> /// 移除实体并提交到数据服务器 /// 如果表存在约束,需要先删除子表信息 /// </summary> /// <param name="item">Item to delete</param> void Delete(TEntity item); /// <summary> /// 修改实体并提交到数据服务器 /// </summary> /// <param name="item"></param> void Update(TEntity item); /// <summary> /// 得到指定的实体集合(延时结果集) /// Get all elements of type {T} in repository /// </summary> /// <returns>List of selected elements</returns> IQueryable<TEntity> GetModel(); /// <summary> /// 根据主键得到实体 /// </summary> /// <param name="id"></param> /// <returns></returns> TEntity Find(params object[] id); }
它会叫每个具体的仓储接口去实现它,如IUserRepository是用户持久化的接口,同时它也可能被持久化基类去实现,如DbContextRepository,它是使用ef来完成持久化的基类,当然你可以使用MemoryContextRepository去实现IRepository这个接口,当然它的功能就是使用内存表来实现持久化的。
抽象类的代码展示:
/// <summary> /// 做为一个持久化机制的实现,它可能是ado.net,linq2sql,entityframeworks,nhibernate,redis,memoryStream,fileStream etc. /// 宗旨:我们不应该将数据持久化的方式暴露到业务(领域)层 /// 建立仓储:为每个聚合根建立仓储接口和实现,而不是为每个实体 /// 使用仓储:应该根据使用场景去声明为仓储,而不是每次都是IExtensionRepository /// </summary> /// <typeparam name="TEntity"></typeparam> public class DemoContextRepository<TEntity> : IExtensionRepository<TEntity> where TEntity : class { #region IExtensionRepository<TEntity>成员 public void Insert(IEnumerable<TEntity> item) { throw new NotImplementedException(); } public void Update(IEnumerable<TEntity> item) { throw new NotImplementedException(); } public void Delete(IEnumerable<TEntity> item) { throw new NotImplementedException(); } public void Update<T>(System.Linq.Expressions.Expression<Action<T>> entity) where T : class { throw new NotImplementedException(); } public IQueryable<TEntity> GetModel(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { throw new NotImplementedException(); } public IQueryable<TEntity> GetModel(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { throw new NotImplementedException(); } public TEntity Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { throw new NotImplementedException(); } public TEntity Find(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { throw new NotImplementedException(); } public void BulkInsert(IEnumerable<TEntity> item, bool isRemoveIdentity) { throw new NotImplementedException(); } public void BulkInsert(IEnumerable<TEntity> item) { throw new NotImplementedException(); } public void BulkUpdate(IEnumerable<TEntity> item, params string[] fieldParams) { throw new NotImplementedException(); } public void BulkDelete(IEnumerable<TEntity> item) { throw new NotImplementedException(); } public event Action<EntityFrameworks.Entity.Core.SavedEventArgs> AfterSaved; public event Action<EntityFrameworks.Entity.Core.SavedEventArgs> BeforeSaved; #endregion #region IOrderableRepository成员 public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy) { throw new NotImplementedException(); } public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { throw new NotImplementedException(); } public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { throw new NotImplementedException(); } #endregion #region IRepository<TEntity>成员 public void Insert(TEntity item) { throw new NotImplementedException(); } public void Delete(TEntity item) { throw new NotImplementedException(); } public void Update(TEntity item) { throw new NotImplementedException(); } public IQueryable<TEntity> GetModel() { throw new NotImplementedException(); } public TEntity Find(params object[] id) { throw new NotImplementedException(); } #endregion }
上面的抽象类只是一个DEMO,所以实现持久化的逻辑我并没有实现,当然这并不是它的重点,重点在于你的具体仓储如果继承了它,将会以DemoContextRepository这种方式去持久化对象。
原文:http://www.cnblogs.com/benlxt/p/3583235.html