Files
Yi.Framework/Yi.Doc.Md/02.框架功能模块/10.工作单元.md
2023-12-15 23:44:35 +08:00

48 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 简介
> 工作单元模式是“维护一个被业务事务影响的对象列表,协调变化的写入和并发问题的解决”
它的作用
1. 事务相关
2. 共用连接
...
## 如何使用
依赖注入`IUnitOfWorkManager`,使用`CreateContext`创建一个`IUnitOfWork`工作单元
在工作单元内部,可提交,回滚,获取仓储
``` cs
bool IsTran { get; set; }
bool IsCommit { get; set; }
bool IsClose { get; set; }
IRepository<T> GetRepository<T>();
bool Commit();
```
> 注意在除Get请求上其他请求默认都开启了工作单元post、put、delelte
## 完整例子
``` cs
private IUnitOfWorkManager _unitOfWorkManager { get; set; }
public void Test()
{
using (var uow = _unitOfWorkManager.CreateContext())
{
//仓储执行各种操作
//统一提交
uow.Commit();
}
}
```
## 特性方式
还可以通过`[UnitOfWork]`特性,打在方法上,该方法便会当作一个事务进行提交
``` cs
[UnitOfWork]
public void Test()
{
//仓储执行各种操作
}
```