当前位置: 首页 > 编程日记 > 正文

UnitOfWork以及其在ABP中的应用

Unit Of Work(UoW)模式在企业应用架构中被广泛使用,它能够将Domain Model中对象状态的变化收集起来,并在适当的时候在同一数据库连接和事务处理上下文中一次性将对象的变更提交到数据中。

从字面上我们可以我们可以把UnitOfWork叫做工作单元,从概念上它是协助代码块的事务。为什么我们需要用UnitOfWork?有人说EF不是的DbContext的SaveChanges不就有提交变更数据的功能吗?为什么还要多一层封装?是的,如果我们项目只是用EF的话,项目又会经常变更,不用考虑那么多我们可以直接用EF,但是如果我们在支持EF的同时还需要支持Redis、NHibernate或MongoDB呢?我们怎么做统一的事务管理?所以封装一个UnitOfWork是有必要的。类似的Repository也是一样,仓储Repository的功能其实就是EF的DbSet<T>,但是我们的数据库访问技术可能会改变,所以我们需要提供一层封装,来隔离领域层或应用层对数据访问层的依赖。那么ABP是怎么定义UnitOfWork的呢?

    public interface IUnitOfWork : IActiveUnitOfWork, IUnitOfWorkCompleteHandle{/// <summary>/// Begins the unit of work with given options./// 开始 unit of work的一些配置UnitOfWorkOptions,主要是事务的级别,超时时间,配置文件等/// </summary>/// <param name="options">Unit of work options</param>void Begin(UnitOfWorkOptions options);}public interface IUnitOfWorkCompleteHandle : IDisposable{/// <summary>/// Completes this unit of work./// It saves all changes and commit transaction if exists./// 统一事务提交/// </summary>void Complete();/// <summary>/// Completes this unit of work./// It saves all changes and commit transaction if exists./// 异步的Complete方法/// </summary>Task CompleteAsync();}

从接口的定义来看,UnitOfWork主要涉及到事务的提交,回滚操作这边没有再定义一个方法,因为作者用的是TransactionScope,失败了会自动回滚。当然有定义了Dispose方法。现在我们来看下UnitOfWork的实现方法,抽象类UnitOfWorkBase,我删除了一些跟本文无关的代码,方便阅读。

    public abstract class UnitOfWorkBase : IUnitOfWork{public UnitOfWorkOptions Options { get; private set; }/// <summary>/// 开始UnitOfWork的一些配置,和事务的初始化/// </summary>/// <param name="options"></param>public void Begin(UnitOfWorkOptions options){if (options == null){throw new ArgumentNullException("options");}PreventMultipleBegin();Options = options; //TODO: Do not set options like that!SetFilters(options.FilterOverrides);BeginUow();}/// <summary>/// 事务的提交,异常的捕获/// </summary>public void Complete(){PreventMultipleComplete();try{CompleteUow();_succeed = true;OnCompleted();}catch (Exception ex){_exception = ex;throw;}}/// <summary>/// 结束事务,失败就回滚/// </summary>public void Dispose(){if (IsDisposed){return;}IsDisposed = true;if (!_succeed){OnFailed(_exception);}DisposeUow();OnDisposed();}}

我们知道UnitOfWorkBase是抽象类,对于不同的数据访问技术方案我们要定义不用的工作单元实现类,比如EF和NHibernate的事务实现机制是不一样的,这里我们看下EfUnitOfWork

    public class EfUnitOfWork : UnitOfWorkBase, ITransientDependency{private readonly IDictionary<Type, DbContext> _activeDbContexts;private readonly IIocResolver _iocResolver;private TransactionScope _transaction;/// <summary>/// Creates a new <see cref="EfUnitOfWork"/>./// </summary>public EfUnitOfWork(IIocResolver iocResolver, IUnitOfWorkDefaultOptions defaultOptions): base(defaultOptions){_iocResolver = iocResolver;_activeDbContexts = new Dictionary<Type, DbContext>();}protected override void BeginUow(){if (Options.IsTransactional == true){var transactionOptions = new TransactionOptions{IsolationLevel = Options.IsolationLevel.GetValueOrDefault(IsolationLevel.ReadUncommitted),};if (Options.Timeout.HasValue){transactionOptions.Timeout = Options.Timeout.Value;}_transaction = new TransactionScope(TransactionScopeOption.Required,transactionOptions,Options.AsyncFlowOption.GetValueOrDefault(TransactionScopeAsyncFlowOption.Enabled));}}public override void SaveChanges(){_activeDbContexts.Values.ForEach(SaveChangesInDbContext);}
...

上面已经定义了UnitOfWork接口和实现方法,那我们改怎么使用呢?一般的我们的使用方式是这样的,下面的场景是模拟银行转账功能,从一个账户扣钱和另一个账户加钱。下面是领域层定义的账户转账服务,我们在整个操作实现完后调用 _unitOfWork.Commit()进行提交,在领域服务构造函数注入UnitOfWork。

    // 账号转账领域服务类public class AccountService{private readonly IAccountRepository _productRepository;private readonly IUnitOfWork _unitOfWork;public AccountService(IAccountRepository productRepository, IUnitOfWork unitOfWork){_productRepository = productRepository;_unitOfWork = unitOfWork;            }public void Transfer(Account from, Account to, decimal amount){if (from.Balance >= amount){from.Balance -= amount;to.Balance += amount;_productRepository.Save(from);_productRepository.Save(to);_unitOfWork.Commit();}} }

这样的设计简单易懂,但是我们每个提交都要引用UnitOfWork会比较麻烦,那么有没有更好的设计思路呢?ABP的设计思想还是比较值得借鉴的。ABP的UnitOfWork的设计思路还是沿用作者最喜欢的切面编程,何为切面编程:通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。也就是AOP技术,ABP作者用的是Castle Windsor来实现的。一般的我们需要两步,1、继承IInterceptor接口重写Intercept方法,这样我们就可以实现动态拦截方法了,2、那么我们到底怎么才能动态代理要拦截的方法呢?我们可以继承Attribute,自定义UnitOfWorkAttribute。可能你现在还不明白,那么我们来看下具体代码吧。

    internal class UnitOfWorkInterceptor : IInterceptor{private readonly IUnitOfWorkManager _unitOfWorkManager;public UnitOfWorkInterceptor(IUnitOfWorkManager unitOfWorkManager){_unitOfWorkManager = unitOfWorkManager;}public void Intercept(IInvocation invocation){if (_unitOfWorkManager.Current != null){//Continue with current uowinvocation.Proceed();return;}var unitOfWorkAttr = UnitOfWorkAttribute.GetUnitOfWorkAttributeOrNull(invocation.MethodInvocationTarget);if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled){//No need to a uowinvocation.Proceed();return;}//No current uow, run a new onePerformUow(invocation, unitOfWorkAttr.CreateOptions());}

对于Castle Windsor我们只需要像上面的UnitOfWorkInterceptor就是继承IInterceptor重写Intercept就可以实现动态代理啦。下面来看下自定义的UnitOfWorkAttribute。

    [AttributeUsage(AttributeTargets.Method)]public class UnitOfWorkAttribute : Attribute{/// <summary>/// Is this UOW transactional?/// Uses default value if not supplied./// </summary>public bool? IsTransactional { get; private set; }/// <summary>/// Timeout of UOW As milliseconds./// Uses default value if not supplied./// </summary>public TimeSpan? Timeout { get; private set; }

好了,定义了UnitOfWorkAttribute,那么我们怎么让它和UnitOfWorkInterceptor结合起来对代码进行动态拦截呢?

        [UnitOfWork]public virtual async Task<AbpLoginResult> LoginAsync(string userNameOrEmailAddress, string plainPassword, string tenancyName = null){if (userNameOrEmailAddress.IsNullOrEmpty()){throw new ArgumentNullException("userNameOrEmailAddress");}if (plainPassword.IsNullOrEmpty()){throw new ArgumentNullException("plainPassword");}using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant)){TUser user;if (!_multiTenancyConfig.IsEnabled){using (_unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MayHaveTenant)){//Log in with default denantuser = await FindByNameOrEmailAsync(userNameOrEmailAddress);if (user == null){return new AbpLoginResult(AbpLoginResultType.InvalidUserNameOrEmailAddress);}}}

上面代码是利用Attribute的特性对方法进行标识,这是第一步,现在我们已经对要拦截的代码标识了,那么我们是怎么知道它要被拦截的呢?

    internal static class UnitOfWorkRegistrar{/// <summary>/// Initializes the registerer./// </summary>sssss/// <param name="iocManager">IOC manager</param>public static void Initialize(IIocManager iocManager){iocManager.IocContainer.Kernel.ComponentRegistered += ComponentRegistered;}/// <summary>/// 拦截注册事件 /// </summary>/// <param name="key"></param>/// <param name="handler"></param>private static void ComponentRegistered(string key, IHandler handler){if (UnitOfWorkHelper.IsConventionalUowClass(handler.ComponentModel.Implementation)){//判断如果是IRepository和IApplicationService,就注册动态代理 Intercept all methods of all repositories.handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));}else if (handler.ComponentModel.Implementation.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(UnitOfWorkHelper.HasUnitOfWorkAttribute)){//判断如果是被标识了UnitOfWork attribute的就注册动态代理 Intercept all methods of classes those have at least one method that has UnitOfWork attribute.//TODO: Intecept only UnitOfWork methods, not other methods!handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));}}}

请认真看上面的方法ComponentRegistered的代码,UnitOfWorkHelper.HasUnitOfWorkAttribute就是判断是否是UnitOfWorkAttribute。

        public static bool HasUnitOfWorkAttribute(MemberInfo methodInfo){return methodInfo.IsDefined(typeof(UnitOfWorkAttribute), true);}

你可能会问UnitOfWorkRegistrar的ComponentRegistered方法是什么时候执行的?那么你可以参考下我之前写的Castle Windsor常用介绍以及其在ABP项目的应用介绍 ,关于UnitOfWorkAttribute 是怎么执行的可以参考ABP之模块分析

那么到此我们被标识[UnitOfWork]的登录方法LoginAsync和所有的repositories仓储一旦被执行到就会被拦截,执行我们的代理类。本来这篇文章还想说说仓储的,但是因为篇幅可能会有点长,那就放在下次总结吧,至此UnitOfWork也就总结到此了。

参考文章:

http://www.cnblogs.com/daxnet/archive/2011/06/03/2071931.html

http://www.cnblogs.com/zhili/p/UnitOfWork.html

http://www.cnblogs.com/xishuai/p/3750154.html

转载于:https://www.cnblogs.com/huaizuo/p/4838680.html

相关文章:

分享3个好用到爆的 Python 模块,点赞收藏

作者 | 俊欣来源 | 关于数据分析与可视化今天给大家介绍3个特别好用的Python模块&#xff0c;知道的人可能不多&#xff0c;但是特别的好用。PsutilPendulumPyfigletPsutilPython当中的Psutil模块是个跨平台库&#xff0c;它能够轻松获取系统运行的进程和系统利用率&#xff0c…

使用XHProf分析PHP性能瓶颈(二)

上一篇文章里&#xff0c;我们介绍了如何基于xhprof扩展来分析PHP性能&#xff0c;并记录到日志里&#xff0c;最后使用xhprof扩展自带的UI在web里展示出来。本篇文章将讲述2个知识点&#xff1a; 使用xhgui代替xhprof的默认UI界面&#xff0c;更便于分析使用tideways扩展替换x…

PHP自动加载类—__autoload()和spl_autoload_register()

test.php <?phpfunction __autoload($class_name) {require_once $class_name . .php;}$obj new j();?> 当前目录下有j.php <?phpclass j{function __construct() {echo "成功加载";} }?> 正常输出&#xff1a;成功加载修改test.php代码<?phpf…

二分 + 模拟 - Carries

Carries Problems Link Mean: 给你n个数&#xff0c;让你计算这n个数两两组合相加的和进位的次数. analyse: 脑洞题. 首先要知道&#xff1a;对于两个数的第k位相加会进位的条件是&#xff1a;a%(10^k)b%(10^k)>10^k. 想到这一点后就简单了&#xff0c;枚举每一位&#…

机器学习的出现,是否意味着“古典科学”的过时?

作者&#xff1a;Laura Spinney译者&#xff1a;刘媛媛原文&#xff1a;Are we witnessing the dawn of post-theory science?让我们回忆一下&#xff0c;Isaac Newton 被一个苹果砸中头部&#xff0c;然后是怎么提出牛顿第二定律——万有引力的&#xff1f;大概过程是这样的&…

MySQL5.6.16二进制源码安装详解及一键安装实现

一、系统环境 1.1操作系统 [rootlocalhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [rootlocalhost ~]# uname -rm 10.0-693.el7.x86_64 x86_64 [rootlocalhost ~]# 1.2 安装前环境监测 1.2.1.SELinux和系统防火墙关闭 检查selinux [rootlocalho…

基于 OpenCV 的表格文本内容提取

作者 | 小白来源 | 小白学视觉小伙伴们可能会觉得从图像中提取文本是一件很麻烦的事情&#xff0c;尤其是需要提取大量文本时。PyTesseract是一种光学字符识别&#xff08;OCR&#xff09;&#xff0c;该库提了供文本图像。PyTesseract确实有一定的效果&#xff0c;用PyTessera…

Redis以及Redis的php扩展安装无错版

安装Redis 下载最新的 官网&#xff1a;http://redis.io/ 或者 http://code.google.com/p/redis/downloads/list第一步&#xff1a;下载安装编译 #wget http://redis.googlecode.com/files/redis-2.4.4.tar.gz#tar zxvf redis-2.4.4.tar.gz#cd redis-2.4.4#make #make instal…

Android UI SurfaceView的使用-绘制组合图型,并使其移动

绘制容器类&#xff1a; //图形绘制容器 public class Contanier {private List<Contanier> list;private float x0,y0;public Contanier(){listnew ArrayList<Contanier>();}public void draw(Canvas canvas){canvas.save();canvas.translate(getX(), getY());chi…

新型混合共识机制及抗量子特性的 Hcash 主链测试链即将上线

由上海交通大学密码与计算机安全实验室&#xff08;LoCCS&#xff09;及上海观源信息科技有限公司负责研发的、具有新型混合共识机制及抗量子特性的 Hcash 主链代码已完成并在 2017 年 12 月18 日之前上传至github&#xff1a; https://github.com/HcashOrg/hcashd https://git…

CentOS 6虚拟机安装

这篇博客已经被合并到这里了&#xff1a; 虚拟机安装CentOS以及SecureCRT设置【完美无错版】 下面不用看了&#xff0c;看上面即可 1.下载虚拟机Oracle VM VirtualBox最新的下载地址&#xff1a; http://download.virtualbox.org/virtualbox/4.1.6/VirtualBox-4.1.6-74713-Win…

开发中新游戏《庞加莱》

三体题材的游戏&#xff0c;表现三体人在三体星上生活和冒险。收集水和物器&#xff0c;躲避火焰与巨日&#xff0c;探索遗迹并与巨型生物战斗。温度会因太阳位置不同而发生变化&#xff0c;进而对环境产生一定影响。 游戏开发中。 ---- 2017-4-27版视频&#xff1a; http://v.…

介绍一个打怪升级练习 Python 的网站,寓教于乐~

作者 | 周萝卜来源 | 萝卜大杂烩这是一个学习 Python 的趣味网站&#xff0c;通过关卡的形式来锻炼 Python 水平。一共有 33 关&#xff0c;每一关都需要利用 Python 知识解题找到答案&#xff0c;然后进入下一关。很考验对 Python 的综合掌握能力&#xff0c;比如有的闯关需要…

hive基本操作与应用

通过hadoop上的hive完成WordCount 启动hadoop ssh localhost cd /usr/local/hadoop ./sbin/start-dfs.sh cd /usr/local/hive/lib service mysql start start-all.sh Hdfs上创建文件夹 hdfs dfs -mkdir test1 hdfs dfs -ls /user/hadoop 上传文件至hdfs hdfs dfs -put ./try.tx…

PHP源代码分析-字符串搜索系列函数实现详解

今天和同事在讨论关键字过虑的算法实现&#xff0c;前几天刚看过布隆过滤算法&#xff0c;于是就想起我们公司内部的查找关键字程序&#xff0c;好奇是怎么实现的。于是查找了一下源代码&#xff0c;原来可以简单地用stripos函数查找&#xff0c; stripos原型如下&#xff1a; …

麻省理工研究:深度图像分类器,居然还会过度解读

作者 | 青苹果来源 | 数据实战派某些情况下&#xff0c;深度学习方法能识别出一些在人类看来毫无意义的图像&#xff0c;而这些图像恰恰也是医疗和自动驾驶决策的潜在隐患所在。换句话说&#xff0c;深度图像分类器可以使用图像的边界&#xff0c;而非对象本身&#xff0c;以超…

Oracle 查询转换之子查询展开

概念:子查询展开&#xff08;Subquery Unnesting&#xff09;是优化器处理带子查询的目标sql的一种优化手段&#xff0c;它是指优化器不再将目标sql中子查询当作一个独立的处理单元来单独执行&#xff0c;而是将该子查询转换为它自身和外部查询之间等价的表连接。这种等价连接转…

Xcode中通过删除原先版本的程序来复位App

可以在Xcode菜单中点击 Product->Clean Build Folder (按住Option键,在windows键盘中是Alt键.) 此时Xcode将会从设备中删除(卸载uninstall)任何该app之前部署的版本. 接下来重启Xcode,再试一下,有时这可以修复非常奇怪(really weird)的问题.

深入理解PHP之OpCode

OpCode是一种PHP脚本编译后的中间语言&#xff0c;就像Java的ByteCode,或者.NET的MSL。 此文主要基于《 Understanding OPcode》和 网络&#xff0c;根据个人的理解和修改&#xff0c;特记录下来 &#xff1a;PHP代码&#xff1a; <?phpecho "Hello World";$a 1…

关于 AIOps 的过去与未来,微软亚洲研究院给我们讲了这些故事

作者 | 贾凯强出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;在过去的15年里&#xff0c;云计算实现了飞速发展&#xff0c;而这种发展也为诸多的前沿技术奠定了基础&#xff0c;AIOps便在此环境中获得了良好的发展契机。在数字化转型的浪潮下&#xff0c;云计算已经…

JS 正则表达式 0.001 ~99.999

^(0|[1-9][0-9]?)(\.[0-9]{0,2}[1-9])?$转载于:https://www.cnblogs.com/wahaha603/p/9050130.html

深入浅出PHP(Exploring PHP)

一直以来&#xff0c;横观国内的PHP现状&#xff0c;很少有专门介绍PHP内部机制的书。呵呵&#xff0c;我会随时记录下研究的心得&#xff0c;有机会的时候&#xff0c;汇总成书。:) 今天这篇&#xff0c;我内心是想打算做为一个导论&#xff1a; PHP是一个被广泛应用的脚本语言…

懒人神器 !一个创意十足的 Python 命令行工具

作者 | 写代码的明哥来源 | Python编程时光当听到某些人说 xx 库非常好用的时候&#xff0c;我们总是忍不住想要去亲自试试。有一些库&#xff0c;之所以好用&#xff0c;是对一些库做了更高级的封闭&#xff0c;你装了这个库&#xff0c;就会附带装了 n 多依赖库&#xff0c;就…

Regular Expression Matching

正则匹配 Regular Expression Matching Implement regular expression matching with support for . and *. . Matches any single character. * Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The functio…

PI校正环节的程序实现推导过程

PI校正环节在经典控制论中非常有用&#xff0c;特别是对负反馈控制系统&#xff0c;基本上都有PI校正环节。1.下面分别说明比例环节和积分环节的作用&#xff0c;以阶跃信号为例。①比例环节单独作用以上分析说明&#xff0c;若只有比例环节的控制系统&#xff0c;阶跃响应也是…

几行 Python 代码实现邮件解析,超赞~

作者 | Yunlor来源 | CSDN博客前言如何通过python实现邮件解析&#xff1f;邮件的格式十分复杂&#xff0c;主要是mime协议&#xff0c;本文主要是从实现出发&#xff0c;具体原理可以自行研究。一、安装通过mailgun开源的Flanker库实现邮件解析。该库包含了邮件地址解析和邮件…

深入理解PHP原理之变量(Variables inside PHP)

或许你知道&#xff0c;或许你不知道&#xff0c;PHP是一个弱类型&#xff0c;动态的脚本语言。所谓弱类型&#xff0c;就是说PHP并不严格验证变量类型(严格来讲&#xff0c;PHP是一个中强类型语言,这部分内容会在以后的文章中叙述)&#xff0c;在申明一个变量的时候&#xff0…

jQuery中的.height()、.innerHeight()和.outerHeight()

jQuery中的.height()、.innerHeight()和.outerHeight()和W3C的盒模型相关的几个获取元素尺寸的方法。对应的宽度获取方法分别为.width()、.innerWidth()和.outerWidth()&#xff0c;在此不详述。1. .height()获取匹配元素集合中的第一个元素的当前计算高度值 或 设置每一个匹配…

Python实战之logging模块使用详解

用Python写代码的时候&#xff0c;在想看的地方写个print xx 就能在控制台上显示打印信息&#xff0c;这样子就能知道它是什么了&#xff0c;但是当我需要看大量的地方或者在一个文件中查看的时候&#xff0c;这时候print就不大方便了&#xff0c;所以Python引入了logging模块来…

深入理解PHP原理之变量作用域

作者:laruence(http://www.laruence.com/)地址: http://www.laruence.com/2008/08/26/463.html PHP变量的内部表示是如何和用户脚本中的变量联系起来的呢&#xff1f;也就是说&#xff0c;如果我在脚本中写下&#xff1a;<?php $var"laruen…