2014-09-11 64 views
0

假設我有2周類似的方法命名的GetFiles()和PutFiles()瘦身多種類似方法的合適方法是什麼?

public static void GetFiles(string source, string dest) 
    { 
     using (Session session = new Session()) 
     { 
      session.Open(sessionOptions); 

      TransferOptions transferOptions = new TransferOptions(); 
      transferOptions.TransferMode = TransferMode.Binary; 

      TransferOperationResult transferResult; 

      ////Different in here//// 
      transferResult = session.GetFiles(source, dest, false, transferOptions); 

      transferResult.Check(); 

      foreach (TransferEventArgs transfer in transferResult.Transfers) 
      { 
       Console.WriteLine("Transfer file from {0} to {1}", source, dest); 
      } 
     } 
    } 

    public static void PutFiles(string source, string dest) 
    { 
     using (Session session = new Session()) 
     { 
      session.Open(sessionOptions); 

      TransferOptions transferOptions = new TransferOptions(); 
      transferOptions.TransferMode = TransferMode.Binary; 

      TransferOperationResult transferResult; 

      ////Different in here//// 
      transferResult = session.PutFiles(source, dest, false, transferOptions); 

      transferResult.Check(); 

      foreach (TransferEventArgs transfer in transferResult.Transfers) 
      { 
       Console.WriteLine("Transfer file from {0} to {1}", source, dest); 
      } 
     } 
    } 

你可以看到他們是除了方法 我有考慮添加一個名爲傳輸文件的新方法中調用不同的功能完全一樣,其中接受一個參數來控制放文件的過程/獲取文件

public static void TransferFile(string source, string dest, bool GetFile) 
    { 
     using (Session session = new Session()) 
     { 
      session.Open(sessionOptions); 

      TransferOptions transferOptions = new TransferOptions(); 
      transferOptions.TransferMode = TransferMode.Binary; 

      TransferOperationResult transferResult; 

      if(GetFile) 
      transferResult = session.GetFiles(source, dest, false, transferOptions); 
      else 
      transferResult = session.PutFiles(source, dest, false, transferOptions); 

      transferResult.Check(); 

      foreach (TransferEventArgs transfer in transferResult.Transfers) 
      { 
       Console.WriteLine("Transfer file from {0} to {1}", source, dest); 
      } 
     } 
    } 

但後來我發現,這不是一個最佳的解決方案,因爲我不能處理超過2條件(獲取文件&放文件)

+0

你是什麼意思,它不能處理超過2個條件? – 2014-09-11 03:22:06

+0

如果我還有一個函數爲 - transferResult = session.CompareFiles(source,dest,false,transferOptions); ,那麼我將不得不將TransferFile(string source,string dest,bool GetFile)的參數更改爲TransferFile(string source,string dest,int FileAction),這樣我可以處理更多不同的過程 – 2014-09-11 03:23:37

+0

但我不認爲這個解決方案是很好所有 – 2014-09-11 03:26:34

回答

0

您可以使用這樣的,

public static void Get_Or_Put_Files(string source, string dest, bool isFilesAreGetting) 
{ 
    using (Session session = new Session()) 
    { 
     session.Open(sessionOptions); 

     TransferOptions transferOptions = new TransferOptions(); 
     transferOptions.TransferMode = TransferMode.Binary; 

     TransferOperationResult transferResult; 

     if(isFilesAreGetting) 
     { 
      transferResult = session.GetFiles(source, dest, false, transferOptions); 
     } 
     else 
     { 
      transferResult = session.PutFiles(source, dest, false, transferOptions); 
     } 

     transferResult.Check(); 

     foreach (TransferEventArgs transfer in transferResult.Transfers) 
     { 
      Console.WriteLine("Transfer file from {0} to {1}", source, dest); 
     } 
    } 
} 

只需添加一個額外的參數來講述這個方法是否獲得或將您的文件。

+0

謝謝,但這就是我所建議的,我想知道是否有更好的解決方案。 – 2014-09-11 06:15:05

+0

但我不這麼認爲,更好的解決方案是存在的比這個。 – 2014-09-11 09:20:57