[Architect] Abp 框架原理解析(5) UnitOfWork
本节目录
介绍
UOW(全称UnitOfWork)是指工作单元.
在Abp中,工作单元对于仓储和应用服务方法默认开启。并在一次请求中,共享同一个工作单元.
同时在Abp中,不仅支持同一个数据库连接,还支持事务处理.
分析Abp源码
1.UnitOfWorkRegistrar
2.ComponentRegistered
3.IsConventionalUowClass
4.Intercept
5.PerformSyncUow
实现UOW
定义IUnitOfWork
public interface IUnitOfWork
{
//1.开启事务
//2.设置Filter(本例中不做演示)
void Begin(UnitOfWorkOptions options); void Complete();
}
public class UnitOfWorkOptions
{
public bool? IsTransactional { get; set; }
}
实现uow,在uow中会提供db的创建,这样才能管理到每个db.
public class EfUnitOfWork : UnitOfWorkBase
{
public static DbContext DbContext { get; set; } public static DbContext GetDbContext()
{
if (DbContext == null)
{
DbContext = new DemoDb();
}
return DbContext;
} public override void Begin(UnitOfWorkOptions options)
{
if (options.IsTransactional == true)
{
CurrentTransaction = new TransactionScope();
}
} public TransactionScope CurrentTransaction { get; set; } public override void Complete()
{
SaveChanges();
if (CurrentTransaction != null)
{
CurrentTransaction.Complete();
}
} private void SaveChanges()
{
DbContext.SaveChanges();
}
}
public abstract class UnitOfWorkBase : IUnitOfWork
{
public virtual void Begin(UnitOfWorkOptions options)
{
} public virtual void Complete()
{
}
}
定义与实现仓储层,这里不再做DbProvider.
public interface IRepository
{ } public interface ITaskRepository : IRepository
{
void Add(Task task);
} public class TaskRepository : ITaskRepository
{
public void Add(Task task)
{
var db = (DemoDb)EfUnitOfWork.GetDbContext();
db.Tasks.Add(task);
}
}
定义与实现应用层
public interface IApplicationService
{ } public interface ITaskAppService : IApplicationService
{
void CreateTask(CreateTaskInput input);
} public class TaskAppService : ITaskAppService
{
private ITaskRepository _repository;
public TaskAppService(ITaskRepository repository)
{
_repository = repository;
}
public void CreateTask(CreateTaskInput input)
{
_repository.Add(new Task(input.Name));
}
} public class CreateTaskInput
{
public string Name { get; set; }
}
定义与实现uow拦截器
internal class UnitOfWorkInterceptor : IInterceptor
{
private IUnitOfWork _unitOfWork;
public UnitOfWorkInterceptor(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void Intercept(IInvocation invocation)
{
_unitOfWork.Begin(new UnitOfWorkOptions());
invocation.Proceed();
_unitOfWork.Complete();
}
}
定义在IApplicationService与IRepository接口下拦截
static void Kernel_ComponentRegistered(string key, Castle.MicroKernel.IHandler handler)
{
var type = handler.ComponentModel.Implementation;
if (typeof(IApplicationService).IsAssignableFrom(type) || typeof(IRepository).IsAssignableFrom(type))
{
handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));
}
}
执行
static void Main(string[] args)
{
using (var container = new WindsorContainer())
{
container.Register(Component.For<IInterceptor, UnitOfWorkInterceptor>());//先注入拦截器
container.Register(Component.For<IUnitOfWork, EfUnitOfWork>());
container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
container.Register(Component.For<ITaskAppService, TaskAppService>());
container.Register(Component.For<ITaskRepository, TaskRepository>());
var person = container.Resolve<ITaskAppService>();
person.CreateTask(new CreateTaskInput() { Name = "3333" });
}
Console.ReadKey();
}
会自动在application method的结尾调用Complete.
另外也可以在uow上定义option为启用事务.在本例中稍作扩展即可实现.
本文地址:http://neverc.cnblogs.com/p/5263558.html
[Architect] Abp 框架原理解析(5) UnitOfWork的更多相关文章
- [Architect] Abp 框架原理解析(4) Validation
本节目录 介绍 DataAnnotations ICustomValidate IShouldNormalize 实现Abp Validation 介绍 Abp中在Application层集成了val ...
- [Architect] Abp 框架原理解析(3) DynamicFilters
本节目录 介绍 定义Filter 设置Filter 这是Abp中多租户.软删除.激活\禁用等如此方便的原因 Install-Package EntityFramework.DynamicFilters ...
- [Architect] Abp 框架原理解析(2) EventBus
本节目录 原理介绍 Abp源码分析 代码实现 原理介绍 事件总线大致原理: (1) 在事件总线内部维护着一个事件与事件处理程序相映射的字典. (2) 利用反射,事件总线会将实现 ...
- [Architect] Abp 框架原理解析(1) Module
本节目录 Abp介绍 Abp源码分析 代码实现 Abp介绍 学习了一段时间的Abp,领略了一下前辈的架构.总结还是SOLID,降低耦合性. 虽然从架构上说甚至不依赖于DI框架,但实际上在基础框架中还是 ...
- [Architect] ABP(现代ASP.NET样板开发框架) 翻译
所有翻译文档,将上传word文档至GitHub 本节目录: 简介 代码示例 支持的功能 GitHub 简介 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目) ...
- ABP架构解析
ABP总体介绍 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- [置顶]
滴滴插件化VirtualAPK框架原理解析(二)之Service 管理
在前一篇博客滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理 中VirtualAPK是如何对Activity进行管理的,本篇博客,我们继续来学习这个框架,这次我们学习的是如何 ...
- ADB运行框架原理解析【转】
本文转载自:http://blog.csdn.net/wlwl0071986/article/details/50935496 一.adb守护进程的初始化 源码路径:~/system/core/adb ...
- Java并发Fork-Join框架原理解析
1.什么是Foirk/Join框架 Fork/Join框架是Java7提供用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 2.什么是并行流与顺 ...
随机推荐
- [算法导论]哈希表 @ Python
直接寻址方式: class HashTable: def __init__(self, length): self.T = [None for i in range(length)] class Da ...
- Console中加入招聘等个性化信息
try { if (window.console && window.console.log) { console.log("%c XX息科技 ", "f ...
- Visual Studio 新建项目报错" this template attempted to load component assembly 'NuGet.VisualStudio.Interop, ….".
"Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version ...
- Mac 下安装mitmproxy
环境: Mac OS X 10.9.4 1. 安装 直接用pip 安装 pip install mitmproxy 发现在安装依赖包 lxml 的时候报错 In : /private/tmp/pip ...
- DRUPAL 慢的原因
不止一次听人抱怨DRUPAL 慢,在本地开发环境尤为常见,较为常见的原因有:- 本地环境造成慢的原因,最常见的是由update manager 造成的,如果你发现你开的DRUPAL 页面 一直在等待 ...
- Mac OS X Tips
命令行查看Mac OS X版本 $ sw_vers ProductName: Mac OS X ProductVersion: BuildVersion: 14D131 Mac OS X截图 不要使用 ...
- 日本超人气洛比(Robi)声控机器人
1.日本超人气洛比(Robi)声控机器人. http://technews.cn/2015/04/18/interview-with-robi-creator-tomotaka-takahashi/ ...
- Linux网络流量实时监控ifstat iftop命令详解
ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB /s i ...
- javaScript数据类型与typeof操作符
1,typeof操作符. typeof操作符是用来检测变量的数据类型.使用:typeof 变量名;返回以下字符串: 字符串 描述 undefined 未定义 boolean 布尔值 string 字 ...
- debian+apache+acme_tiny+lets-encrypt配置笔记
需要预先将需要申请ssl的域名指向到服务器,此方法完全通过api实现,好处是绿色无污染,不需要注册账号,不会泄露私人信息环境为 debian7+apache apt-get install apach ...