2011-10-19 18 views
0

我使用的是asp.net mvc和nhibernate工作模式的單元。我應該如何設計? Nhibernate工作單元中的一種方法

我有這樣的事情

public bool IsSomething() 
{ 
    unitOfWork.BeginTransaction(); 
    var myGetStuff = repo.GetStuff(1); 

    if(myGetStuff == null) 
    { 
     return false; 
    } 

    var somethingElse = myGetStuff.GetSomethingElse(); 

    if(somethngElse == null) 
    { 
     return false; 
    } 

    return true; 
} 

所以在我的if語句,如果事情是零,我需要它不爲空我剛走出語句。

這是相反的嵌套if語句可能像嵌套4或5次做空檢查。

public bool IsSomething() 
{ 
    unitOfWork.BeginTransaction(); 
    var myGetStuff = repo.GetStuff(1); 

    if(myGetStuff != null) 
    { 
     var somethingElse = myGetStuff.GetSomethingElse(); 

     if(somethngElse != null) 
     { 
      // some more check heere (could have another few if statements null checks here) 
     } 
    } 
} 

因此,我發現第一種方法更容易閱讀嵌套級別的if語句。

我的問題是,即使你在nhibernate中做了一個查詢,你也要必須把它包裝在一個事務中,最後做一個回滾或提交。

選項1

public bool IsSomething() 
    { 
     unitOfWork.BeginTransaction(); 
     var myGetStuff = repo.GetStuff(1); 

     if(myGetStuff == null) 
     { 
      unitOfWork.Commit(); 
      return false; 
     } 

     var somethingElse = myGetStuff.GetSomethingElse(); 

     if(somethngElse == null) 
     { 
     unitOfWork.Commit(); 
      return false; 
     } 

     unitOfWork.Commit(); 
     return true; 
    } 

這樣,你必須一直把承諾無論我不喜歡。如果可能的話我希望只有一個提交(除非我有工作事務的多個單元)

於是我雖然爲什麼不把它放在最後這樣

public bool IsSomething() 
    { 
    try 
    { 
      unitOfWork.BeginTransaction(); 
      var myGetStuff = repo.GetStuff(1); 

      if(myGetStuff == null) 
      { 
       return false; 
      } 

      var somethingElse = myGetStuff.GetSomethingElse(); 

      if(somethngElse == null) 
      { 

       return false; 
      } 

      return true; 
     } 
     catch(Exception ex) 
     { 
      unitOfWork.RollBack(); 
     } 
     finally 
     { 
      unitOfWork.Commit(); 
     } 
    } 

我喜歡這一點,但那麼我意識到如果提交失敗會發生什麼?它不會回滾,異常不會被捕獲。

那麼其他人有什麼想法?

+0

你只讀還是你更新/插入也? – Firo

+0

取決於一些可能有讀/更新/插入混合的方法,或者有些可能只是讀取。 – chobo2

回答

1

這段代碼對我來說相當麻煩,特別是當嵌套的工作調用單元時(你如何處理這些代碼?)。我會做的是在調用代碼中打開工作單元(和事務)(在您使用ASP.Net MVC的情況下爲Controller),而不是IsSomething函數。這將是這個樣子:

try 
{ 
    unitOfWork.BeginTransaction(); 
    // some code 
    var isSomething = IsSomeThing() 
} 
catch(Exception ex) 
{ 
    unitOfWork.RollBack(); 
} 
finally 
{ 
    unitOfWork.Commit(); 
} 

的IsSomething功能會那麼看上去簡直像這樣

public bool IsSomething() 
{ 
    var myGetStuff = repo.GetStuff(1); 

    if(myGetStuff == null) 
    { 
     return false; 
    } 

    var somethingElse = myGetStuff.GetSomethingElse(); 

    if(somethngElse == null) 
    { 

     return false; 
    } 

    return true; 
} 
+0

我不確定嵌套的工作單元是什麼。這是我曾考慮過但不確定的事情。 IsSomething在我所有的業務邏輯所在的服務層。我設置我的服務層,以便它在自己的項目中,並試圖儘可能獨立於mvc,以便我可以將該項目用於不同的項目(比如web服務)。因此,如果服務層不是我製作的.dll,然後突然你必須將它包裝在UoW中,那將會很好。那麼我不確定一個UoW應該首先在一個控制器中嗎? – chobo2

+0

如果您需要多個事務,那麼在您的方案中會發生什麼情況? – chobo2

+0

我的意思是嵌套的工作單位如下:你有一段代碼(讓我們說一個控制器)。在開始時,您將調用UoW.BeginTransaction(),並在最後調用UoW.Commit。然後在這個控制器內部調用你的IsSomething函數,它也有一個UoW.Commit。這不會干擾你的第一個UoW.BeginTransaction?我所做的是始終在http模塊中啓動,提交和回滾我的事務。 – zszep