2015-12-16 16 views
0

我有一個類似於下面顯示的代碼,我想知道是否有一個更有效的方法來編寫此方法在mvc。什麼是在C#中嵌套if語句的更有效方法?

public ActionResult LogViewerFiles([DataSourceRequest] DataSourceRequest request, string testValue, string testValue2) 

    { 
     if (string.IsNullOrWhiteSpace(testValue2) && string.IsNullOrWhiteSpace(testValue)) 
     { 
      return View(); 
     } 
     if (!string.IsNullOrWhiteSpace(testValue)) 
     { 
      var fileName = testValue; 
      var logEntries = LogEntry.ReadLogEntries(fileName); 

      if (logEntries == null) 
      { 
       return JavaScript("Callback()");     
      } 
      return Json(logEntries.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
     } 
     if (testValue2!= null) 
     { 
      var resultPath = testValue2; 
      var logEntriesArch = LogEntry.ReadLogEntries(resultPath); 
      return Json(logEntriesArch.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
     } 
     return null; 
    } 

基本上我試圖實現的是執行不同的if語句,這取決於我的參數的值。在這種情況下,只有一個參數會有我需要執行代碼的信息,而另一個參數將始終爲空。這種方法是這樣編寫的,因此所有收集的信息都可以顯示在劍道剃刀網格中。

+6

定義「高效」。 – Albireo

+0

那麼你不需要'fileName'或'resultPath'變量。只要將'testValue'和'testValue2'傳遞給'LogEntry.ReadLogEntries'。 – juharr

+8

我發現你的代碼有很多問題需要重構。如果它編譯並正在工作,我建議你在http://codereview.stackexchange.com/上發佈它。 –

回答

1

雖然矯枉過正(你的確實在你的代碼中存在設計問題),但你可以在調度器中重構你的代碼。

請參見本LINQPad例如:

public void Main() 
{ 
    Console.WriteLine(this.YourAction("foo", "bar")); 
    Console.WriteLine(this.YourAction("foo", "baz")); 
} 

public string YourAction(string firstParameter, string secondParameter) 
{ 
    var handler = Dispatcher.GetHandler(firstParameter, secondParameter); 

    if (handler == null) 
    { 
     throw new Exception("I don't know what to do."); 
    } 

    return handler.Handle(firstParameter, secondParameter); 
} 

public static class Dispatcher 
{ 
    private readonly static ICollection<Handler> Handlers = new List<Handler>(); 

    static Dispatcher() 
    { 
     Handlers.Add(new FooHandler()); 
     Handlers.Add(new BarHandler()); 
    } 

    public static Handler GetHandler(string firstParameter, string secondParameter) 
    { 
     // Yes, Single, not First. If you screw up and two handlers can handle 
     // the same parameters set you want to know it up front, not later. 
     return Handlers.SingleOrDefault(a => a.CanHandle(firstParameter, secondParameter)); 
    } 
} 

public abstract class Handler 
{ 
    public abstract bool CanHandle(string firstParameter, string secondParameter); 

    public abstract string Handle(string firstParameter, string secondParameter); 
} 

public class FooHandler : Handler 
{ 
    public override bool CanHandle(string firstParameter, string secondParameter) 
    { 
     return firstParameter == "foo" && secondParameter == "bar"; 
    } 

    public override string Handle(string firstParameter, string secondParameter) 
    { 
     return "FooAction"; 
    } 
} 

public class BarHandler : Handler 
{ 
    public override bool CanHandle(string firstParameter, string secondParameter) 
    { 
     return firstParameter == "foo" && secondParameter == "baz"; 
    } 

    public override string Handle(string firstParameter, string secondParameter) 
    { 
     return "BarAction"; 
    } 
} 

每個處理器告訴調度員,如果它能夠處理所提供的參數,調度員挑選正確的,然後運行它。

+0

這可能看起來有點過分,但我同意如果你需要在你的控制器中運行嵌套條件來找出真正需要執行的操作,那麼你要麼有一個非常複雜的系統,並且可能需要一個調度器,或者你需要重新考慮你的架構真的很糟糕。 – kai