分享基于Entity Framework的Repository模式设计(附源码)
2013-09-07 23:53
330 查看
分享基于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对数据的修改,需要在统一的保存.
最终实现的类结构图如下:
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
相关文章推荐
- 分享基于Entity Framework的Repository模式设计(附源码)
- 分享基于Entity Framework的Repository模式设计(附源码)
- 分享基于Entity Framework的Repository模式设计
- 基于设计模式的学习之旅-----访问者模式(附源码)
- 基于设计模式的学习之旅-----适配器模式(附源码)
- 基于设计模式的学习之旅-----命令模式(附源码)
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- 基于设计模式的学习之旅-----中介者(附源码)
- 论坛源码推荐(8月14日):基于高德地图iOS SDK的轨迹回放库,用Swift实现的设计模式
- 基于Android设计模式之--SDK源码之策略模式的详解
- 基于设计模式的学习之旅-----责任链(附源码)
- 基于GridView LoadMoreAsync 设计的横向双向瀑布流 组件 源码分享
- 《GOF设计模式》—抽象工厂(Abstract Factory)—Delphi源码示例:基于类的工厂
- 基于ASP.NET WPF技术及MVP模式实战太平人寿客户管理项目开发(Repository模式)课程分享
- 基于设计模式的学习之旅-----观察者模式(附源码)
- 基于设计模式的学习之旅-----状态模式(附源码)
- 基于设计模式的学习之旅-----外观模式(附源码)
- 基于设计模式的学习之旅-----模板方法模式(附源码)
- 基于设计模式的学习之旅-----享元模式(附源码)
- 基于面向对象(OO)的数据库设计模式探讨,第 2 部分