關於Repository模式,直接百度查就可以了,其來源是《企業應用架構模式》。我們新建一個Infrastructure文件夾,這裡就是基礎設施部分,EF Core的上下文類以及Repository層都放在這裡面。新建一個IReposotory的介面,其內容就是封裝了基本的CRUD: public ...
關於Repository模式,直接百度查就可以了,其來源是《企業應用架構模式》。
我們新建一個Infrastructure文件夾,這裡就是基礎設施部分,EF Core的上下文類以及Repository層都放在這裡面。
新建一個IReposotory的介面,其內容就是封裝了基本的CRUD:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public interface IRepository<TEntity> where TEntity : class { ///獲取當前實體的查詢數據集 IQueryable<TEntity> Entities{get;} ///獲取當前實體的數據集 DbSet<TEntity> DbEntities{get;} /// <summary> /// Gets all objects from database /// </summary> /// <returns></returns> IQueryable<TEntity> All(); /// <summary> /// Gets objects from database by filter. /// </summary> /// <param name="predicate">Specified a filter</param> /// <returns></returns> IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate); /// <summary> /// Gets objects from database with filting and paging. /// </summary> /// <param name="filter">Specified a filter</param> /// <param name="total">Returns the total records count of the filter.</param> /// <param name="index">Specified the page index.</param> /// <param name="size">Specified the page size</param> /// <returns></returns> IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50); /// <summary> /// Gets the object(s) is exists in database by specified filter. /// </summary> /// <param name="predicate">Specified the filter expression</param> /// <returns></returns> bool Contains(Expression<Func<TEntity, bool>> predicate); /// <summary> /// Find object by keys. /// </summary> /// <param name="keys">Specified the search keys.</param> /// <returns></returns> TEntity Find(params object[] keys); /// <summary> /// Find object by specified expression. /// </summary> /// <param name="predicate"></param> /// <returns></returns> TEntity Find(Expression<Func<TEntity, bool>> predicate); /// <summary> /// Create a new object to database. /// </summary> /// <param name="t">Specified a new object to create.</param> /// <returns></returns> int Create(TEntity t); /// <summary> /// Delete the object from database. /// </summary> /// <param name="t">Specified a existing object to delete.</param> void Delete(TEntity t); /// <summary> /// Delete objects from database by specified filter expression. /// </summary> /// <param name="predicate"></param> /// <returns></returns> int Delete(Expression<Func<TEntity, bool>> predicate); /// <summary> /// Update object changes and save to database. /// </summary> /// <param name="t">Specified the object to save.</param> /// <returns></returns> int Update(TEntity t); /// <summary> /// Select Single Item by specified expression. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <returns></returns> TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression); }View Code
創建一個基類,用來實現IRepository介面,同時作其餘的Repository的基類
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class { protected readonly DbContext Context; public BaseRepository(DbContext context) { Context = context; } /// 獲取當前實體的查詢數據集 public IQueryable<TEntity> Entities { get { return Context.Set<TEntity>().AsQueryable(); } } /// 獲取當前實體 public IQueryable<TEntity> Entities { get { return Context.Set<TEntity>(); } } public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression) { return All().FirstOrDefault(expression); } public IQueryable<TEntity> All() { return Context.Set<TEntity>().AsQueryable(); } public virtual IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate) { return Context.Set<TEntity>().Where<TEntity>(predicate).AsQueryable<TEntity>(); } public virtual IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50) { var skipCount = index * size; var resetSet = filter != null ? Context.Set<TEntity>().Where<TEntity>(filter).AsQueryable() : Context.Set<TEntity>().AsQueryable(); resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size); total = resetSet.Count(); return resetSet.AsQueryable(); } public virtual int Create(TEntity TObject) { Entities.Add(TObject); Context.SaveChanges(); } public virtual int Delete(TEntity TObject) { Entities.Remove(TObject);. Context.SaveChanges(); } public virtual void Update(TEntity TObject) { try { var entry = Context.Entry(TObject); Context.Set<TEntity>().Attach(TObject); entry.State = EntityState.Modified; } catch (OptimisticConcurrencyException ex) { throw ex; } } public virtual int Delete(Expression<Func<TEntity, bool>> predicate) { var objects = Filter(predicate); foreach (var obj in objects) Context.Set<TEntity>().Remove(obj); return Context.SaveChanges(); } public bool Contains(Expression<Func<TEntity, bool>> predicate) { return Context.Set<TEntity>().Any(predicate); } public virtual TEntity Find(params object[] keys) { return Context.Set<TEntity>().Find(keys); } public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate) { return Context.Set<TEntity>().FirstOrDefault<TEntity>(predicate); } }View Code
新建一個實體類的介面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public interface IStudentRepository : IRepository<Student> { int AddStudent(Student student); }View Code
然後我們創建一個實體類的Repository:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class StudentRepository : BaseRepository<Student>, IStudentRepository { private readonly SchoolContext _context; public StudentRepository(SchoolContext context) : base(context) { _context = context; } int AddStudent(Student student) { _context.Create(student); } }View Code
在這裡就已經做好了我們要做的了。接下來的就是註入依賴、在控制器裡面的使用了。
我們完全可以自己來定製自己的Repository模式下的項目。其實整個的架構沒有什麼,我們只是將所有的CRUD操作封裝到了IRepository介面裡面,然後在BaseRepository中實現了一遍,而且如果你細心的話,你會發現IRepository裡面的CRUD操作都是基於已有的擴展方法裡面的,就是linq擴展方法的Add等源碼,同時我們在BaseRepository類中,提供了DbSet<TEntity>屬性以及查詢數據集IQueryable<DbSet<TEntity>>,這也是有必要的,可以省卻我們很多不必要的代碼,因為我們所有的CRUD都是基於這兩個的。然後我們基於BaseRepository來實現實體類的Repository,同時繼承按需增加的IEntityRepository介面。但是在這裡要註意,我們將DbContext的子類都放在了Infrastructure文件夾裡面,是因為,一般我們繼承自DbContext的子類都是操作資料庫的中間類,屬於基礎設施一塊,所以將其放在Infrastructure文件夾比較合適。
參考資料:
MVC實用架構設計(三)——EF-Code First(1):Repository,UnitOfWork,DbContext
分享基於Entity Framework的Repository模式設計(附源碼)
《ASP.NET MVC框架揭秘》源碼中的示例項目源碼 S1402