Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加工作单元手动关闭 #1

Merged
merged 1 commit into from
May 9, 2019
Merged

添加工作单元手动关闭 #1

merged 1 commit into from
May 9, 2019

Conversation

stulzq
Copy link
Collaborator

@stulzq stulzq commented May 9, 2019

调用此方法后,工作单元将失效,即不会开启事务,即便仓储赋值了 Repo.UnitOfWork=uow 也不会开启事务。

添加此方法不会影响以前代码的正常使用。

在已有 Insert/Update/Delete 以后调用此方法将会抛出异常,因为这时已经开启事务,不能使工作单元生效。

应用场景:类似于ABP的在应用逻辑层的自动工作单元(自动开启事务),利用AOP在执行方法以前开启工作单元,执行完毕以后自动提交,但是此功能可以设置Attribute来进行关闭。本方法就是能是AOP在执行方法前通过检测Attribute来进行关闭工作单元。因为目前需要给每个仓储赋值工作单元过于麻烦,所以在依赖注入仓储时,可以封装默认就注入工作单元,类似于

class Repo<T>:BaseRepo<T>
{
    public Repo(IUnitOfWork uow)
    {
        this.UnitOfWork=uow
    }
}

这样实际上在使用仓储的时候就赋值了工作单元,可以理解为默认开启了工作单元,但是我们肯定有不需要开启工作单元的情况,所以设置一个关闭的方法。

class AppService
{
    private readonly Repo<T> _repo
    public AppService(Repo<T> repo)
    {
        _repo=repo
    }

   [UnitOfWork(false)] // AOP 调用 uow.Disable()
    public void Business()
    {
        _repo.Insert(xxx)
    }

    //因为在依赖注入已经为仓储赋值uow 所以,uow默认开启
    public void Business2()
    {
        _repo.Insert(xxx)
    }
   //AOP uow.Commit
}

单元测试已通过:

image

@2881099
Copy link
Owner

2881099 commented May 9, 2019

增加 IUnitOfWork.Disable() 禁用事务开启的方法,以及单元测试

@2881099 2881099 merged commit dba122b into 2881099:master May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants