2016-01-14 92 views
0
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注入的類的私有成員。UnitOfWork模式的嵌套事務問題

我無法從Operation01中刪除Commit(),因爲它在應用程序中被稱爲獨立操作。 如果在Operation02()上發生任何錯誤,我想回滾所有更改。

回答

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(); 
} 
+0

任何其他方式,更優雅... –

+0

魔法?這很簡單,你可以得到。 – dbugger

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 
    }); 
} 

也許不完全是你之後,但你明白了。