目录

  • 分享基于EF6、Unitwork、Autofac的Repository模式设计

    • 一、实现的思路和结构图
    • 二、Repository设计具体的实现代码
    • 三、Repository设计的具体的使用
    • 四、思路总结
    • 五、案例源码

分享基于EF6、Unitwork、Autofac的Repository模式设计

一、实现的思路和结构图

  • Repository的共同性

有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository,然后有个基类BaseRepository实现该接口的方法。常见的方法,比如Find, Filter, Delete, Create等

  • Repository的差异性

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

  • Repository的协同性

    不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.
    最终实现的类结构图如下:

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

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

  • IRepository代码
    public interface IRepository<TEntity> where TEntity : class{/// <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 filtering 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>void 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>void Update(TEntity t);/// <summary>/// Select Single Item by specified expression./// </summary>/// <param name="expression"></param>/// <returns></returns>TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression);}
  • BaseRepository代码
    public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class{protected readonly DbContext Context;public BaseRepository(DbContext context){Context = context;}/// <summary>/// Gets all objects from database/// </summary>/// <returns></returns>public IQueryable<TEntity> All(){return Context.Set<TEntity>().AsQueryable();}/// <summary>/// Gets objects from database by filter./// </summary>/// <param name="predicate">Specified a filter</param>/// <returns></returns>public virtual IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate){return Context.Set<TEntity>().Where<TEntity>(predicate).AsQueryable<TEntity>();}/// <summary>/// Gets objects from database with filtering 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>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();}/// <summary>/// Gets the object(s) is exists in database by specified filter./// </summary>/// <param name="predicate">Specified the filter expression</param>/// <returns></returns>public bool Contains(Expression<Func<TEntity, bool>> predicate){return Context.Set<TEntity>().Any(predicate);}/// <summary>/// Find object by keys./// </summary>/// <param name="keys">Specified the search keys.</param>/// <returns></returns>public virtual TEntity Find(params object[] keys){return Context.Set<TEntity>().Find(keys);}/// <summary>/// Find object by specified expression./// </summary>/// <param name="predicate"></param>/// <returns></returns>public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate){return Context.Set<TEntity>().FirstOrDefault<TEntity>(predicate);}/// <summary>/// Create a new object to database./// </summary>/// <param name="t">Specified a new object to create.</param>/// <returns></returns>public virtual void Create(TEntity t){Context.Set<TEntity>().Add(t);}/// <summary>/// Delete the object from database./// </summary>/// <param name="t">Specified a existing object to delete.</param>public virtual void Delete(TEntity t){Context.Set<TEntity>().Remove(t);}/// <summary>/// Delete objects from database by specified filter expression./// </summary>/// <param name="predicate"></param>/// <returns></returns>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();}/// <summary>/// Update object changes and save to database./// </summary>/// <param name="t">Specified the object to save.</param>/// <returns></returns>public virtual void Update(TEntity t){try{var entry = Context.Entry(t);Context.Set<TEntity>().Attach(t);entry.State = EntityState.Modified;}catch (OptimisticConcurrencyException ex){throw ex;}}/// <summary>/// Select Single Item by specified expression./// </summary>/// <param name="expression"></param>/// <returns></returns>public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression){return All().FirstOrDefault(expression);}}

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

  • IUnitOfWork接口代码:
    public interface IUnitOfWork : IDisposable{DbContext DbContext { get; }TRepository GetRepository<TRepository>() where TRepository : class;void ExecuteProcedure(string procedureCommand, params object[] sqlParams);void ExecuteSql(string sql);List<T> SqlQuery<T>(string sql);void SaveChanges();}

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

    public class UnitOfWork : IUnitOfWork{private readonly IComponentContext _componentContext;protected readonly DbContext Context;public UnitOfWork(DbContext context, IComponentContext componentContext){Context = context;_componentContext = componentContext;}public DbContext DbContext => Context;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 ExecuteSql(string sql){Context.Database.ExecuteSqlCommand(sql);}public List<T> SqlQuery<T>(string sql){return Context.Database.SqlQuery<T>(sql).ToList();}public void SaveChanges(){try{Context.SaveChanges();}catch (InvalidOperationException ex){if (!ex.Message.Contains("The changes to the database were committed successfully")){throw;}}}public void Dispose(){Context?.Dispose();}}

三、Repository设计的具体的使用

这里我们定义一个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;}
}
  • Application_Start方法中使用Autofac注册Repository的代码如下:
    var builder = new ContainerBuilder();//register controllersbuilder.RegisterControllers(typeof(MvcApplication).Assembly);//register repositorybuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();//add the Entity Framework context to make sure only one context per requestbuilder.RegisterType<SchoolContext>().InstancePerRequest();builder.Register(c => c.Resolve<SchoolContext>()).As<DbContext>().InstancePerRequest();var container = builder.Build();DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  • 在控制器中注入使用Repository的代码如下:
private readonly IUnitOfWork _repositoryCenter;private readonly IStudentRepository _studentRepository;public HomeController(IUnitOfWork repositoryCenter)
{_repositoryCenter = repositoryCenter;_studentRepository = _repositoryCenter.GetRepository<IStudentRepository>();
}public ActionResult Index(Student sessionStudent)
{var students = _studentRepository.GetAllStudents();// 同时你也可以使用定义于IRepository<Student>中的方法, 比如:_studentRepository.Delete(students.First());_repositoryCenter.SaveChanges();...return View(students);
}

四、思路总结

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

只是现在的设计和Autofac耦合了,但是如果想继续剥离Autofac直接使用 _repositoryCenter.GetRepository<IStudentRepository>(); 的方式获取IStudentRepository的实例就很困难了。

五、案例源码

源代码仓库 AutoFacMvc

转载于:https://www.cnblogs.com/Run2948/p/11212770.html

分享基于EF6、Unitwork、Autofac的Repository模式设计相关推荐

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

    关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable? 这篇文章介绍的是使用Entity Framework实现的Rep ...

  2. [源码和文档分享]基于java语言的C/S模式网络聊天室软件

    一 需求分析 采用C/S模式,基于TCP协议编程的方式,使得各个用户通过服务器转发实现聊天的功能 分为三大模块:客户端模块.服务器端模块和公共辅助类模块 客户端模块的主要功能: 登陆功能:用户可以注册 ...

  3. [源码和文档分享]基于Java语言的C/S模式通讯录备份和查询软件

    一 需求分析 本设计要求完成一个基于C/S模式的通讯录备份软件,采用C/S架构,具有易用.美观的图形界面. 1.1 服务器端功能要求 能够验证客户身份,接收客户端的备份通讯录的请求,能够实时备份和更新 ...

  4. android ble 助手源码_[源码和文档分享]基于Android的生活助手APP的设计与实现

    摘 要 随着移动互联网的高速发展,Android操作系统在移动设备中地位已经被牢牢稳固.然而大量的Android设备高速普及过程中,与其配套的Android应用的开发速度和项目质量极为令人担忧.本课题 ...

  5. [源码和文档分享]基于Android的生活助手APP的设计与实现

    摘 要 随着移动互联网的高速发展,Android操作系统在移动设备中地位已经被牢牢稳固.然而大量的Android设备高速普及过程中,与其配套的Android应用的开发速度和项目质量极为令人担忧.本课题 ...

  6. [源码和文档分享]基于Android平台的个人理财软件的设计与实现

    摘要 个人理财管理系统是基于Android系统开发的一款手机应用程序.它主要是为了满足人们在快节奏的生活中可以随时记下自己的收支情况的需求.个人理财管理系统与传统的记账方式相比,体现了它的便捷性.安全 ...

  7. mfc 怎么让键盘上下左右控制图片移动_[源码和文档分享]基于MFC的陨石撞飞机游戏设计与实现...

    摘 要 用MFC设计一个陨石撞飞机的平面游戏,陨石不断下落,飞机通过键盘的上下左右键移动以躲避陨石.当陨石撞到飞机时,显示游戏结束提示对话框.设计开始要对开发环境VC 6.0的熟悉,需要学会如何添加资 ...

  8. python下俄罗斯方块的游戏设计_[源码和文档分享]基于Python的PyGame的俄罗斯方块游戏设计与实现...

    摘 要 近年来,随着游戏产业的突飞猛进,游戏玩家的技术也是与日俱增,当你看见游戏高手完美的表演时,你是否想过我也能达到那种水平,本程序用Python语言编写俄罗斯方块,左侧显示正在运行的游戏,右边显示 ...

  9. [源码和文档分享]基于C语言的小球移动课程设计

    一.需求分析 用C语言实现"小球移动"的简单图形游戏.可添加.删除小球,小球的分数和大小随机,球会在游戏区域内反弹,小球可被删除,删除时球上的数字累加到玩家的分数上.还可实现暂停. ...

最新文章

  1. mybatis中![CDATA[]]的作用
  2. Linux系统.xsesion日志文件,linux系统日志
  3. angular元素属性绑定_AngularJS语法基础及数据绑定——详解各种数据绑定指令、属性应用...
  4. 关于提高代码复用性的几个知识点的回顾
  5. mongodb转实体对像_MongoDB:实体对象(javabean)转DBObject
  6. UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position 14: illegal multibyte sequence
  7. 当自己颓废的时候怎么激励自己?
  8. 《计算机网络》学习笔记 ·001【概述】
  9. windows 7 提示缺少D3DCOMPILER_47.dll的正确解决方法
  10. Linux程序文件状态,linux 文件状态标识和IO操作方式
  11. sqlmap详细使用教程
  12. HP M1136打印机 Mac驱动程序分享
  13. 用java判断是否是合法邮箱即验证邮箱格式
  14. K均值 - 案例实现(python)
  15. Java学历很重要_Java开发找工作,学历重要还是技术重要?
  16. 取代 Ant:使用 Maven 管理 Wowza 插件开发
  17. 苹果系统模拟器_开发者成功在苹果MacBook上通过模拟器正常运行Windows 10X版
  18. mysqlin会使用索引吗
  19. mysql程序语句范文_MySQL基本语句
  20. 云课堂计算机测试答案,2020智慧职教云课堂计算机应用基础答案最新最全章节测试答案...

热门文章

  1. node+express+MongoDB实现小商城服务端
  2. webpack dev server 和 sublime text 配合时需要注意的地方
  3. 时间一天一天过去,很快;时间如果过的慢,更是没有意思
  4. 2673(2673)shǎ崽 OrOrOrOrz
  5. GPS定位精度单位CEP、RMS、2DRMS
  6. 计算机文档里的东西可以删吗,电脑c盘哪些文件可以删除
  7. c语言funcode空格消失的函数,01北科大暑期计算机实践FunCode游戏设计+C++课程设计 - 海底世界 - 图文...
  8. url参数传递 java_URL中文参数传递问题
  9. python异步生成器
  10. 利用人脑神经突触进行网络模型剪枝