UnitOfWork Pattern的嵌套事务问题

时间:2016-01-14 14:08:32

标签: design-patterns unit-of-work

public void Operation01()
{
    //Some Data manipulation here.
    this.UnitOfWork.Commit();
}

public void Operation02()
{
    Operation01();

    //Some db work here

    //Some ERROR Occurs here, but operation 1 is commited.

    this.UnitOfWork.Commit();  
}

UnitOfWork是使用IOC通过框架注入的类的私有成员。

我无法从Operation01中删除Commit(),因为它有时在应用程序中被称为独立操作。 如果在Operation02()上发生任何错误,我想回滚每个更改。

2 个答案:

答案 0 :(得分:1)

将您的工作推向单独的方法......

public void Operation01()
{
    Operation01Worker();
    this.UnitOfWork.Commit();
}

private void Operation01Worker()
{
    //Some Data manipulation here.
}

public void Operation02()
{
    Operation01Worker();

    //Some db work here

    this.UnitOfWork.Commit();  
}

答案 1 :(得分:0)

与代表这样的事情怎么样:

public void Operation01(Action doThisToo = null)
{
    //Some Data manipulation here.

    if (!doThisToo == null)
    {
        doThisToo;
    }

    this.UnitOfWork.Commit();

}

public void Operation02()
{
    Operation01(() =>
    {
        //Some db work here
    }); 
}

也许不完全是你所追求的,但你明白了。