zoukankan      html  css  js  c++  java
  • 分享基于Entity Framework的Repository模式设计(附源码)

    分享基于Entity Framework的Repository模式设计(附源码)

    关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable? 

    这篇文章介绍的是使用Entity Framework实现的Repositoy模式设计,欢迎各位拍砖.

    阅读目录:

    一、实现的思路和结构图

    二、Repository设计具体的实现代码

    三、Repository设计的具体的使用

    四、总结

    一,实现的思路和结构图

    总结一下,Repository在实际使用中,有下面三种特点:

    Repository的共同性

    有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository<TEntity>, 然后有个基类BaseRepository<TEntity>实现该接口的方法。

    常见的方法,比如Find, Filter, Delete, Create等

    Repository的差异性

    每个Repository类又会有一些差异性,应当允许它们能够继承BaseRepository<TEntity>之外,还能够再扩展自己的一些方法。所以每个类都可以再定义一个自己特有的接口,定义一些属于自己Repository的方法。

    Repository的协同性

    不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.

    最终实现的类结构图如下:

    image

    二,Repository设计具体的实现代码

    IRepository<TEntity>接口定义了Repository共有的方法, BaseRepository<TEntity>实现了这些接口的方法。其它的Repository类再集成BaseRepository<TEntity>方法,就天然的得到了对数据操作的基本方法。

    IRepository<TEntity>代码

    复制代码
    public interface IRepository<TEntity> where TEntity : class
    {
          IQueryable<TEntity> All();
          IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate);
          IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50);
          bool Contains(Expression<Func<TEntity, bool>> predicate);
          TEntity Find(params object[] keys);
          TEntity Find(Expression<Func<TEntity, bool>> predicate);
          TEntity Create(TEntity t);
          int Delete(TEntity t);
          int Delete(Expression<Func<TEntity, bool>> predicate);
          int Update(TEntity t);
          TEntity Single(Expression<Func<TEntity, bool>> expression);
    }
    复制代码

    BaseRepository<TEntity>代码

     View Code

    IUnitOfWorkRepository接口定义了方法获取特定的Repository, 执行存储过程, SaveChange方法提交修改,统一更新数据。

    IUnitOfWorkRepository接口代码:

    复制代码
    public interface IUnitOfWorkRepository : IDisposable
    {
          TRepository GetRepository<TRepository>() where TRepository : class;
          void ExecuteProcedure(string procedureCommand, params object[] sqlParams);
          void SaveChanges();
    }
    复制代码

    UnitOfWorkRepository代码, 代码中使用到了Autofac中的IComponentContext来获取Repository实例

    复制代码
    public class UnitOfWorkRepository : IUnitOfWorkRepository
    {
           private readonly IComponentContext _componentContext;
           protected readonly DbContext Context;
    
           public UnitOfWorkRepository(DbContext context, IComponentContext componentContext)
           {
               Context = context;
               _componentContext = componentContext;
           }
    
           public TRepository GetRepository<TRepository>() where TRepository : class
           {
               return _componentContext.Resolve<TRepository>();
           }
    
           public void ExecuteProcedure(string procedureCommand, params object[] sqlParams)
           {
               Context.Database.ExecuteSqlCommand(procedureCommand, sqlParams);
           }
    
           public void SaveChanges()
           {
               Context.SaveChanges();
           }
    
           public void Dispose()
           {
               if (Context != null)
                   Context.Dispose();
           }
    }
    复制代码

    三, Repository设计的具体的使用

    这里我们定义一个操作Student的Repository类,看看如何实际用于开发中。这里加入StudentRepository有自己特定的方法,需要获取所有的Students,这个扩展的方法名字叫GetAllStudents

    那么定义一个接口IStudentRepository, 包含了方法GetAllStudents(), 同时继承IRepository<Student>接口

    public interface IStudentRepository : IRepository<Student>
    {
          IEnumerable<dynamic> GetAllStudents();
    }

    然后定义StudentRepository类来实现这个接口

    复制代码
    public class StudentRepository : BaseRepository<Student>, IStudentRepository
    {
           private readonly SchoolContext _context;
    
           public StudentRepository(SchoolContext context)
               : base(context)
           {
               _context = context;
           }
    
           public IEnumerable<dynamic> GetAllStudents()
           {
               return _context.Students;
           }
    }
    复制代码

    使用Repository的代码如下:

    复制代码
    IUnitOfWorkRepository unitOfWorkRepository = new UnitOfWorkRepository();
    
    var studentRepository = unitOfWorkRepository.GetRepository<IStudentRepository>();
    var students = studentRepository.GetAllStudents();
    
    //同时你也可以使用定义于IRepository<Student>中的方法, 比如
    //unitOfWorkRepository.Delete(students.First());
    //unitOfWorkRepository.SaveChanges();
    复制代码

    四,总结

    上面的设计,把Repository的通用代码剥离到父类中,同时又允许每个Repository扩展自己的方法,达到了比较理想的状态。

    只是现在的设计和Autofac耦合了,但是想剥离Autofac的话,直接使用下面的方式获取IStudentRepository的实例就很困难。

    unitOfWorkRepository.GetRepository<IStudentRepository>();

    如果有什么好的办法,欢迎指教。也欢迎各位拍砖。

    最后,附上本文的相关源代码. RepositoryDesign.zip

  • 相关阅读:
    Hadoop学习---Zookeeper+Hbase配置学习
    Hadoop学习---Hadoop的HBase的学习
    Hadoop学习---Hadoop的MapReduce的原理
    Hadoop学习---Hadoop的深入学习
    Hadoop学习---Eclipse中hadoop环境的搭建
    Hadoop学习---CentOS中hadoop伪分布式集群安装
    Hadoop学习---Ubuntu中hadoop完全分布式安装教程
    大数据学习---大数据的学习【all】
    Java实例---flappy-bird实例解析
    UML类图详细介绍
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3307950.html
Copyright © 2011-2022 走看看