首先,重构的想法来源于以下文章:Correct use of Repository and Unit Of Work patterns in ASP.NET MVC,因为我发现在我的框架中,对UnitOfWork使用了错误的设计方法,同时感谢一下文章:Generically Implementing the Unit of Work & Repository Pattern with Entity Framework in MVC & Simplifying Entity Graphs,我的重构设计参考了它。

下面来说说我的具体重构方式。

在原来的代码中,我的Repository<T>泛型继承自IRepository<T>并且在增删改查代码中做了如下处理:

   1:    public virtual void Insert(T entity)
   2:          {
   3:              try
   4:              {
   5:                  if (entity == null)
   6:                      throw new ArgumentException("实体类为空");
   7:                  DbSet.Add(entity);
   8:                  context.SaveChanges();
   9:              }
  10:              catch (DbEntityValidationException dbex)
  11:              {
  12:                  var msg = string.Empty;
  13:                  foreach(var validationErrors in dbex.EntityValidationErrors)
  14:                      foreach(var validateionError in validationErrors.ValidationErrors)
  15:                          msg+=string.Format("Property:{0} Error:{1}",validateionError.PropertyName,validateionError.ErrorMessage);
  16:   
  17:                  var fail = new Exception(msg,dbex);
  18:                  throw fail;
  19:              }
  20:          }

最关键的地方是,我添加了“Context.SaveChanges”方法,这就直接导致UnitOfWork的规则失效。UnitOfWork出现的本身是为了提供事务提交支持。这样直接在Repository中提交,直接导致UnitOfWork功能废弃。

还有个地方就是在前台,通过Autofac注册完毕后,我是这么用的:

   1:   public BookService(IUnitOfWork unitOfWork
   2:              , IBook bookRepository
   3:              , IBookType bookTypeRepository
   4:              , IBookPlace bookPlaceRepository
   5:              , ICacheManager cacheManager
   6:              , ILoggerService logger
   7:              )
   8:          {
   9:              this.unitOfWork = unitOfWork;
  10:              this.bookRepository = bookRepository;
  11:              this.bookTypeRepository = bookTypeRepository;
  12:              this.bookPlaceRepository = bookPlaceRepository;
  13:              this.cacheManager = cacheManager;
  14:              this.logger = logger;
  15:          }
  16:   
  17:          private readonly IUnitOfWork unitOfWork;
  18:          private readonly IBook bookRepository;
  19:          private readonly IBookType bookTypeRepository;
  20:          private readonly IBookPlace bookPlaceRepository;
  21:          private readonly ICacheManager cacheManager;
  22:          private readonly ILoggerService logger;

这样做的话,当以后我们有表删除或者新增的时候,我们不得不维护这样的列表。这完全不符合OO设计原则。

但是如果引入UnitOfWork的话,内部利用Hashtable等实现对Respository的指向,那么在界面我们只要这样写就可以了:

   1:   public BookService(
   2:                IUnitOfWork unitOfWork
   3:              , ICacheManager cacheManager
   4:              , ILoggerService logger
   5:              )
   6:          {
   7:              this.unitOfWork = unitOfWork;
   8:              this.cacheManager = cacheManager;
   9:              this.logger = logger;
  10:          }
  11:   
  12:          private readonly IUnitOfWork unitOfWork;
  13:          private readonly ICacheManager cacheManager;
  14:          private readonly ILoggerService logger;

使用的时候,直接这样实例化就行了:

   1:  var bookPlaceRepository = unitOfWork.Repository<BookPlace>();

这样做完全不用顾虑有新表的添加删除了。代码根本就不用动。

所以,综上两点,UnitOfWork的引入为了解决以下问题:

1.提供全局事务支持。

2.提供对Repository模型的指向。以便于松耦合绑定。

下面是代码重构部分:

IUnitOfWork接口部分:

   1:  using System;
   2:  using TinyFrame.Data.DataRepository;
   3:  using TinyFrame.Data.DomainModel;
   4:   
   5:  namespace TinyFrame.Unitofwork
   6:  {
   7:      public interface IUnitOfWork
   8:      {
   9:          void Commit();
  10:          IRepository<T> Repository<T>() where T : class;
  11:   
  12:          void Dispose(bool disposing);
  13:          void Dispose();
  14:      }
  15:  }

实现部分比较简单,利用Hashtable来保存对Repository的指向:

   1:  using System;
   2:  using System.Data.Entity;
   3:  using TinyFrame.Data.DataRepository;
   4:  using TinyFrame.Data.DomainModel;
   5:  using TinyFrame.Data.DataContext;
   6:  using System.Collections;
   7:   
   8:  namespace TinyFrame.Unitofwork
   9:  {
  10:      public class UnitOfWork : IUnitOfWork
  11:      {
  12:          public UnitOfWork(IDbContext dbContext)
  13:          {
  14:              this.dbContext = dbContext;
  15:          }
  16:   
  17:          private readonly IDbContext dbContext;
  18:          private bool disposed;
  19:          private Hashtable repositorys;
  20:   
  21:          public void Commit()
  22:          {
  23:              dbContext.SaveChanges();
  24:          }
  25:   
  26:          public IRepository<T> Repository<T>() where T:class
  27:          {
  28:              if (repositorys == null)
  29:                  repositorys = new Hashtable();
  30:   
  31:              var type = typeof(T).Name;
  32:   
  33:              if (!repositorys.ContainsKey(type))
  34:              {
  35:                  var repositoryType = typeof(Repository<>);
  36:                  var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), dbContext);
  37:                  repositorys.Add(type, repositoryInstance);
  38:              }
  39:              return (IRepository<T>)repositorys[type];
  40:          }
  41:   
  42:          #region Dispose method
  43:          public virtual void Dispose(bool disposing)
  44:          {
  45:              if (!disposed)
  46:                  if (disposing) 
  47:                      dbContext.Dispose();
  48:              disposed = true;
  49:          }
  50:          
  51:          public void Dispose()
  52:          {
  53:              Dispose(true);
  54:              GC.SuppressFinalize(this);
  55:          }
  56:          #endregion
  57:      }
  58:  }

第17行,保持对DbContext的引用,以便于进行提交操作。

第26行,Repository<T>泛型方法,以便于动态返回仓储模型。

需要注意的是,在Repository<T>的实现中,不要再增删改查里面再添加 DbContext.SaveChanges方法,首先是没意义,其次是完全不符合Repository和UnitOfWork的做法。

最后附图一张,表明我对Repository和UnitOfWork的理解:

本章源码下载:

点击这里下载

备份下载地址

TinyFrame升级之七:重构Repository和Unit Of Work的更多相关文章

  1. MVC3+EF4.1学习系列(八)-----利用Repository and Unit of Work重构项目

    项目最基础的东西已经结束了,但是现在我们的项目还不健全  不利于测试 重复性代码多   层与层之间耦合性高  不利于扩展等问题.今天的这章 主要就是解决这些问题的.再解决这些问题时,自己也产生了很多疑 ...

  2. 在Entity Framework 4.0中使用 Repository 和 Unit of Work 模式

    [原文地址]Using Repository and Unit of Work patterns with Entity Framework 4.0 [原文发表日期] 16 June 09 04:08 ...

  3. 转载Repository 和Unit of work的使用说明

    利用Repository and Unit of Work重构项目 文章索引和简介 项目最基础的东西已经结束了,但是现在我们的项目还不健全  不利于测试 重复性代码多   层与层之间耦合性高  不利于 ...

  4. TinyFrame升级之一:框架概览

    由于之前的TinyFrame多于简单,并且只是说明原理,并无成型的框架出来,所以这次我把之前的知识进行了汇总,然后做出了这一版的TinyFrame框架. 整个框架的结构如下: TinyFrame.Da ...

  5. Using the Repository and Unit Of Work Pattern in .net core

    A typical software application will invariably need to access some kind of data store in order to ca ...

  6. TinyFrame升级之三:逻辑访问部分

    在上一篇,我们打造了自己的数据访问部分,这篇,我们准备讲解如何打造逻辑访问部分. 在上一篇中,我们利用Repository模式构建了基于泛型的操作合集.由于这些操作的合集都是原子性的操作,也就是针对单 ...

  7. TinyFrame升级之四:IOC容器

    在这个框架中,我们使用Autofac作为IOC容器,来实现控制反转,依赖注入的目的. 在程序加载的时候,我需要将系统中所有用到的接口与之对应的实现进行装载.由于用户交互部分是在TinyFrame.We ...

  8. TinyFrame升级之九:实现复杂的查询

    本章我们主要讲解如何实现一个复杂的查询.由于目前TinyFrame框架已经投入到了实际的项目生产中,所以我很乐意将项目中遇到的任何问题做以记录并备忘. 这章中,我们提到的查询界面如下所示: 其中,涉及 ...

  9. TinyFrame升级之十:WCF Rest Service注入IOC的心

    由于在实际开发中,Silverlight需要调用WebService完成数据的获取,由于之前我们一直采用古老的ASMX方式,生成的代理类不仅难以维护,而且自身没有提供APM模式的调用方式,导致在Sin ...

随机推荐

  1. mysql 判断表字段或索引是否存在

    判断字段是否存在: DROP PROCEDURE IF EXISTS schema_change; DELIMITER // CREATE PROCEDURE schema_change() BEGI ...

  2. WPF学习之路(六)Command

    在WPF中,命令绑定机制是相比于事件更高级的概念,把应用程序的功能划分为多个任务,任务由多种途径触发. 应用Command Binding使代码更符合MVVM模式(Model-View-ViewMod ...

  3. JavaScript Patterns 5.6 Static Members

    Public Static Members // constructor var Gadget = function (price) { this.price = price; }; // a sta ...

  4. sql server使用中遇到的问题记录

    一.sql server 不能连接远程服务器,但可以连接本地的数据库 我目前用的是sql server 2012 sp1,用着用着突然就不能连接远程服务器上的数据库了,崩溃了一天... 修复试了,卸载 ...

  5. Symantec Backup Exec 报"Access denied to directory xxx" Error Code E0008488

    使用Symantec Backup Exec将几台Linux服务器上的RMAN备份收带时,偶尔会遇到作业备份失败的情况,检查Job History,就会发现有“Access denied to dir ...

  6. Druid 介绍及配置

    1. Druid是什么? Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. 2. 在哪里下载druid 正式版本下载:maven中央仓库: http://cent ...

  7. mybatis 中#{}与${}的区别

    1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111&qu ...

  8. EF深入系列--细节

    1.在调试的时候,查看EF生成的SQL语句 在Context类的构造函数中添加以下代码,就可以在调试的时候在[输出]窗口中看到SQL语句 this.Database.Log = s => Sys ...

  9. JUnit4生命周期

  10. make

    make会自动搜索当前目录下的makefile或Makefile文件进行编译,也可以通过-f选项读取其他文件. make [-abvijm etc] -C dir表示到dir指定的路径去搜索文件 -f ...