代码改变世界

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

2013-09-07 20:59  JustRun  阅读(19694)  评论(51编辑  收藏  举报

关于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对数据的修改,需要在统一的保存.

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

二,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);
      void Delete(TEntity t);
      void Delete(Expression<Func<TEntity, bool>> predicate);
      void Update(TEntity t);
      TEntity Single(Expression<Func<TEntity, bool>> expression);
}

BaseRepository<TEntity>代码

 public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext Context;

        public BaseRepository(DbContext context)
        {
            Context = context;
        }

        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 void Create(TEntity TObject)
        {
            Context.Set<TEntity>().Add(TObject);
        }

        public virtual void Delete(TEntity TObject)
        {
            Context.Set<TEntity>().Remove(TObject);
        }

        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

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

IUnitOfWork接口代码:

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

 

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

public class UnitOfWork : IUnitOfWork
{
       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的代码如下:

IUnitOfWork unitOfWork = new UnitOfWork();

var studentRepository = unitOfWork.GetRepository<IStudentRepository>();
var students = studentRepository.GetAllStudents();

//同时你也可以使用定义于IRepository<Student>中的方法, 比如
//unitOfWork.Delete(students.First());
//unitOfWork.SaveChanges();

四,总结

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

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

unitOfWorkRepository.GetRepository<IStudentRepository>();

 

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

 

最后,附上本文的相关源代码. RepositoryDesign.zip有朋友反映这个设计有问题,希望大家批判的看待。如果有不同看法,欢迎指教。

五, 反馈及更新

感谢热心的园友提供的意见。 这个Repository的设计是不成熟的,在使用了一段EF和看了一些文章之后,自己的一些探索和思考,还没有应用到实际项目中。

Eric.Chen我写的不是代码 是寂寞, Vincent Yang 中提到的UnitOfWork问题,已经修改好.

郭明锋 提到的Single方法不合适,已经改成FirstOrDefault()

最新的源代码 RepositoryDesign1.zip