铺垫

通常在使用 EntityFramework 时,我们会封装出 IRepository 和 IUnitOfWork 接口,前者负责 CRUD 操作,后者负责数据提交 Commit。

   public interface IRepository<T>
where T : class
{
IQueryable<T> Query(); void Insert(T entity); void Update(T entity, params Expression<Func<T, object>>[] modifiedPropertyLambdas); void Delete(T entity);
}
   public interface IUnitOfWork
{
void Commit();
}

然后,通过使用 Unity IoC 容器来注册泛型接口与实现类型。

       Func<IUnityContainer> factory = () =>
{
return new UnityContainer()
.RegisterType(typeof(IRepository<>), typeof(Repository<>), new ContainerControlledLifetimeManager())
.RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager())
.RegisterType<DbContext, MyDBContext>(new ContainerControlledLifetimeManager())
.RegisterType<DbContextAdapter>(new ContainerControlledLifetimeManager())
.RegisterType<IObjectSetFactory, DbContextAdapter>(new ContainerControlledLifetimeManager())
.RegisterType<IObjectContext, DbContextAdapter>(new ContainerControlledLifetimeManager());
};

进而使与数据库相关的操作在 Bisuness Logic 中呈现的非常简单。

例如,通过一系列封装,我们可以达到如下效果:

         Customer customer = new Customer()
{
ID = "",
FirstName = "Dennis",
LastName = "Gao",
};
Repository.Customer.Insert(customer);
Repository.Commit();

查询操作也是一句话搞定:

 Customer customer = Repository.Customer.Query().SingleOrDefault(c => c.ID == "");

需求

假设有一个新的需求:要求在应用层面记录对每个 Table 的 CRUD 的次数。

这时,有几种办法:

  1. 应用程序的 Business Logic 中自己记录,比如调用 Update() 操作后记录。
  2. 使用 AOP 模式,在调用 CRUD 方法时注入计数器。
  3. 修改 Repository<T> 实现,在每个方法中嵌入计数器。
  4. 继承 Repository<T> 类,在衍生类中嵌入计数器。
  5. 使用装饰器模式封装 Repository<T>,在新的 RepositoryDecorator<T> 类中嵌入计数器。

考虑到前三种方法均需要改动已有代码,主要是涉及的修改太多,所有没有尝试采用。

方法 4 则要求修改 Repository<T> 的实现,为 CRUD 方法添加 virtual 关键字以便扩展。

方法 5 不需要修改 Repository<T> 的实现,对已有代码的改动不大。

综上所述,我们选择了方法 5。

Repository 装饰器基类实现

为便于以后的扩展,创建一个装饰器的抽象类。

   public abstract class RepositoryDecorator<T> : IRepository<T>
where T : class
{
private readonly IRepository<T> _surrogate; protected RepositoryDecorator(IRepository<T> surrogate)
{
_surrogate = surrogate;
} protected IRepository<T> Surrogate
{
get { return _surrogate; }
} #region IRepository<T> Members public virtual IQueryable<T> Query()
{
return _surrogate.Query();
} public virtual void Insert(T entity)
{
_surrogate.Insert(entity);
} public virtual void Update(T entity, params Expression<Func<T, object>>[] modifiedPropertyLambdas)
{
_surrogate.Update(entity, modifiedPropertyLambdas);
} public virtual void Delete(T entity)
{
_surrogate.Delete(entity);
} #endregion
}

可以看到,RepositoryDecorator<T> 类型仍然实现了 IRepository<T> 接口,对外使用没有任何变化。

实现需求

我们定义一个 CountableRepository<T> 类用于封装 CRUD 计数功能,其继承自 RepositoryDecorator<T> 抽象类。

   public class CountableRepository<T> : RepositoryDecorator<T>
where T : class
{
public CountableRepository(IRepository<T> surrogate)
: base(surrogate)
{
} public override IQueryable<T> Query()
{
PerformanceCounter.CountQuery<T>();
return base.Query();
} public override void Insert(T entity)
{
PerformanceCounter.CountInsert<T>();
base.Insert(entity);
} public override void Update(T entity, params Expression<Func<T, object>>[] modifiedPropertyLambdas)
{
PerformanceCounter.CountUpdate<T>();
base.Update(entity, modifiedPropertyLambdas);
} public override void Delete(T entity)
{
PerformanceCounter.CountDelete<T>();
base.Delete(entity);
}
}

我们在 override 方法中,添加了 CRUD 的计数功能。这里的代码简写为:

 PerformanceCounter.CountQuery<T>();

对原有代码的修改则是需要注册新的 CountableRepository<T> 类型。

       Func<IUnityContainer> factory = () =>
{
return new UnityContainer()
.ReplaceBehaviorExtensionsWithSafeExtension()
.RegisterType(typeof(IRepository<>), typeof(Repository<>), new ContainerControlledLifetimeManager())
.RegisterType(typeof(CountableRepository<>), new ContainerControlledLifetimeManager())
.RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager())
.RegisterType<DbContext, MyDBContext>(new ContainerControlledLifetimeManager())
.RegisterType<DbContextAdapter>(new ContainerControlledLifetimeManager())
.RegisterType<IObjectSetFactory, DbContextAdapter>(new ContainerControlledLifetimeManager())
.RegisterType<IObjectContext, DbContextAdapter>(new ContainerControlledLifetimeManager());
};

扩展应用

既然有了抽象基类 RepositoryDecorator<T> ,我们可以从其设计衍生多个特定场景的 Repository 。

比如,当我们需要为某个 Table 的 Entity 添加缓存功能时,我们可以定制一个 CachableRepository<T> 来完成这一个扩展。

EntityFramework中使用Repository装饰器的更多相关文章

  1. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  2. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...

  3. python 中多个装饰器的执行顺序

    python 中多个装饰器的执行顺序: def wrapper1(f1): print('in wrapper1') def inner1(*args,**kwargs): print('in inn ...

  4. 第7.17节 Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析

    第7.17节  Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析 静态方法也是通过类定义的一种方法,一般将不需要访问类属性但是类需要具有的一些能力可以静态方法提供. 一 ...

  5. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  6. Python中的各种装饰器详解

    Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...

  7. Python中闭包、装饰器的概念

    1.闭包(Closure)的概念: 内部函数中对enclosing作用域的变量进行引用 1 passline = 60 2 def func(val): 3 print('%x' % id(val)) ...

  8. 谈谈Python中的decorator装饰器,如何更优雅的重用代码

    众所周知,Python本身有很多优雅的语法,让你能用一行代码写出其他语言很多行代码才能做的事情,比如: 最常用的迭代(eg: for i in range(1,10)), 列表生成式(eg: [ x* ...

  9. Python 中写一个装饰器实现限制频率访问

    1.思路: 首先要在装饰器中确定访问的方法名, 第一次可以访问成功,之后要在规定的时间(变量)之后才可以访问. 初始应该有一个变量为0;访问成功之后把当前的时间赋值给这个变零. 这样再次访问时把当前的 ...

随机推荐

  1. Python学习笔记(三)数据类型

    在内存中存储的数据可以有多种类型,在Python中,能够直接处理的数据类型有以下几种: 数字(Numbers) 字符串(String) 列表(List) 元组(Tuple) 字典(Dictionary ...

  2. information_schema系列之字符集校验(CHARACTER_SETS,COLLATIONS,COLLATION_CHARACTER_SET_APPLICABILITY)

    1:CHARACTER_SETS 首先看一下查询前十条的结果: root@localhost [information_schema]>select * from CHARACTER_SETS ...

  3. Android 下拉刷新框架实现

    原文地址:http://blog.csdn.net/leehong2005/article/details/12567757 前段时间项目中用到了下拉刷新功能,之前在网上也找到过类似的demo,但这些 ...

  4. 转 苹果的新编程语言 Swift 简介

    苹果官方文档地址 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Pro ...

  5. 【POJ2949】Word Rings(最大平均值环)

    题意:给定N个字符串,如果A串的最后两个字母跟B串的前两个字母相同它们就能连接. 求一个由字符串组成的首尾相连的环,使(字符串总长度/字符串个数)最大. n<=100000 len<=10 ...

  6. POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

    通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...

  7. 使用spring 4.0 + maven 构建超简单的web项目

    一.需求 使用spring去管理web项目,是目前非常流行的一种思路,本文将介绍使用maven+spring 4.0.2 来构建一个简单的web项目. 二.实现 1.新建一个maven项目,如下图所示 ...

  8. sbt的assembly插件使用(打包所有依赖)

    1.sbt是什么 对于sbt 我也是小白, 为了搞spark看了一下scala,学习scala时指定的构建工具就是sbt(因为sbt也是用scala开发的嘛),起初在我眼里就是一个maven(虽然ma ...

  9. JS 菜单栏一直悬浮在顶部代码

    只需要把下面代码放到js中: $(function(){                //获取要定位元素距离浏览器顶部的距离         var navH = $(".menu&quo ...

  10. MySQL Batch 与 Transaction

    最近在数据库上经常遇到死锁问题. 表现的问题有 1. 有一个查询为: 1) 一个复杂的 select 查处一组大数据 2) 使用事务 update 这组数据的状态 为了让锁定的时间变短, 我将这整个大 ...