基于Repository模式设计项目架构—你可以参考的项目架构设计
关于Repository模式,直接百度查就可以了,其来源是《企业应用架构模式》。
我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repository层都放在这里面。
新建一个IReposotory的接口,其内容就是封装了基本的CRUD:
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); }
创建一个基类,用来实现IRepository接口,同时作其余的Repository的基类
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); } }
新建一个实体类的接口:
public interface IStudentRepository : IRepository<Student> { int AddStudent(Student student); }
然后我们创建一个实体类的Repository:
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); } }
在这里就已经做好了我们要做的了。接下来的就是注入依赖、在控制器里面的使用了。
我们完全可以自己来定制自己的Repository模式下的项目。其实整个的架构没有什么,我们只是将所有的CRUD操作封装到了IRepository接口里面,然后在BaseRepository中实现了一遍,而且如果你细心的话,你会发现IRepository里面的CRUD操作都是基于已有的扩展方法里面的,就是linq扩展方法的Add等源码,同时我们在BaseRepository类中,提供了DbSet<TEntity>属性以及查询数据集IQueryable<DbSet<TEntity>>,这也是有必要的,可以省却我们很多不必要的代码,因为我们所有的CRUD都是基于这两个的。然后我们基于BaseRepository来实现实体类的Repository,同时继承按需增加的IEntityRepository接口。但是在这里要注意,我们将DbContext的子类都放在了Infrastructure文件夹里面,是因为,一般我们继承自DbContext的子类都是操作数据库的中间类,属于基础设施一块,所以将其放在Infrastructure文件夹比较合适。
参考资料:
《ASP.NET MVC框架揭秘》源码中的示例项目源码 S1402