分享基于Entity Framework的Repository模式设计(附源码)
2017-10-19 11:53
453 查看
关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable?
这篇文章介绍的是使用Entity Framework实现的Repositoy模式设计,欢迎各位拍砖.
阅读目录:
一、实现的思路和结构图
二、Repository设计具体的实现代码
三、Repository设计的具体的使用
四、总结
Repository的共同性
有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository<TEntity>, 然后有个基类BaseRepository<TEntity>实现该接口的方法。
常见的方法,比如Find, Filter, Delete, Create等
Repository的差异性
每个Repository类又会有一些差异性,应当允许它们能够继承BaseRepository<TEntity>之外,还能够再扩展自己的一些方法。所以每个类都可以再定义一个自己特有的接口,定义一些属于自己Repository的方法。
Repository的协同性
不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.
最终实现的类结构图如下:
IUnitOfWork接口定义了方法获取特定的Repository, 执行存储过程, SaveChange方法提交修改,统一更新数据。
IUnitOfWork接口代码:
UnitOfWork代码, 代码中使用到了Autofac中的IComponentContext来获取Repository实例
那么定义一个接口IStudentRepository, 包含了方法GetAllStudents(), 同时继承IRepository<Student>接口
然后定义StudentRepository类来实现这个接口
使用Repository的代码如下:
只是现在的设计和Autofac耦合了,但是想剥离Autofac的话,直接使用下面的方式获取IStudentRepository的实例就很困难。
unitOfWorkRepository.GetRepository<IStudentRepository>();
如果有什么好的办法,欢迎指教。也欢迎各位拍砖。
最后,附上本文的相关源代码. RepositoryDesign.zip有朋友反映这个设计有问题,希望大家批判的看待。如果有不同看法,欢迎指教。
Eric.Chen,我写的不是代码 是寂寞, Vincent Yang 中提到的UnitOfWork问题,已经修改好.
郭明锋 提到的Single方法不合适,已经改成FirstOrDefault()
最新的源代码 RepositoryDesign1.zip
这篇文章介绍的是使用Entity Framework实现的Repositoy模式设计,欢迎各位拍砖.
阅读目录:
一、实现的思路和结构图
二、Repository设计具体的实现代码
三、Repository设计的具体的使用
四、总结
一,实现的思路和结构图
总结一下,Repository在实际使用中,有下面三种特点:Repository的共同性
有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository<TEntity>, 然后有个基类BaseRepository<TEntity>实现该接口的方法。
常见的方法,比如Find, Filter, Delete, Create等
Repository的差异性
每个Repository类又会有一些差异性,应当允许它们能够继承BaseRepository<TEntity>之外,还能够再扩展自己的一些方法。所以每个类都可以再定义一个自己特有的接口,定义一些属于自己Repository的方法。
Repository的协同性
不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.
最终实现的类结构图如下:
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); } }
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
相关文章推荐
- 分享基于Entity Framework的Repository模式设计(附源码)
- 分享基于Entity Framework的Repository模式设计(附源码)
- 分享基于Entity Framework的Repository模式设计
- 基于设计模式的学习之旅-----模板方法模式(附源码)
- 论坛源码推荐(8月14日):基于高德地图iOS SDK的轨迹回放库,用Swift实现的设计模式
- 基于设计模式的学习之旅-----适配器模式(附源码)
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- 基于设计模式的学习之旅-----命令模式(附源码)
- 基于设计模式的学习之旅-----访问者模式(附源码)
- 基于设计模式的学习之旅-----中介者(附源码)
- 基于Android设计模式之--SDK源码之策略模式的详解
- 基于ASP.NET WPF技术及MVP模式实战太平人寿客户管理项目开发(Repository模式)课程分享
- 基于设计模式的学习之旅-----责任链(附源码)
- 基于GridView LoadMoreAsync 设计的横向双向瀑布流 组件 源码分享
- 《GOF设计模式》—抽象工厂(Abstract Factory)—Delphi源码示例:基于类的工厂
- 基于设计模式的学习之旅-----观察者模式(附源码)
- 基于设计模式的学习之旅-----状态模式(附源码)
- 基于设计模式的学习之旅-----外观模式(附源码)
- 基于设计模式的学习之旅-----享元模式(附源码)
- 基于Java的代理设计模式