2009-01-06 46 views
3

所以我重構了我繼承的遺留代碼庫,並且在此過程中,我找到了一個靜態類,它封裝了啓動第三方應用程序的邏輯。它本質上是這樣的(縮短爲簡潔起見,只顯示一個應用程序):.NET類重構窘境

using System.IO; 
using System.Configuration; 
public static class ExternalApplications 
{ 

    public string App1Path 
    { 
     get 
     { 
     if(null == thisApp1Path) 
      thisApp1Path = Configuration.AppSettings.Get("App1Path"); 
     return thisApp1Path; 
     } 
    } 
    private string thisApp1Path = null; 

    public bool App1Exists() 
    { 
     if(string.IsNullOrEmpty(App1Path)) 
     throw new ConfigurationException("App1Path not specified."); 
     return File.Exists(App1Path); 
    } 

    public void ExecuteApp1(string args) 
    { 
     // Code to launch the application. 
    } 

} 

這是一個不錯的嘗試分離從代碼的其餘部分的外部應用程序,但它發生,我認爲這可能是重構進一步。我想到的是這樣的:

using System.IO; 
public abstract class ExternalApplicationBase 
{ 

    protected ExternalApplicationBase() 
    { 
     InitializeFromConfiguration(); 
    } 

    public string Path { get; protected set; } 

    public bool Exists() 
    { 
     if(string.IsNullOrEmpty(this.Path)) 
     throw new ConfigurationException("Path not specified."); 
     return File.Exists(this.Path); 
    } 

    public virtual void Execute(string args) 
    { 
     // Implementation to launch the application 
    } 

    protected abstract InitializeFromConfiguration(); 

} 

public class App1 : ExternalApplicationBase 
{ 

    protected virtual void InitializeFromConfiguration() 
    { 
     // Implementation to initialize this application from 
     // the application's configuration file. 
    } 

} 

public class App2 : ExternalApplicationBase 
{ 

    protected virtual void InitializeFromConfiguration() 
    { 
     // Implementation to initialize this application from 
     // the application's configuration file. 
    } 

} 

我關注的是如下:

  1. 類,接口,或其他結構可能已經存在,做這個,我只是避風港」絆倒了它。

  2. 對於我想要做的事情,這可能是矯枉過正的。但是,請注意,該應用程序至少使用了三個獨立的第三方應用程序,這些應用程序目前爲止我們已經確定(並且更多幾乎肯定會彈出)。

  3. 我對基類的名字不太滿意。它看起來很模糊,而且信息量不大(但由於應用程序已經很好的定義,由框架保留,並且在我使用它的時候會產生嚴重的混亂),所以我想不出更好。

  4. 這個想法是我希望能夠在App.Config文件中保留應用程序配置數據(它的路徑和可執行文件名),並在我的應用程序啓動時檢查它的存在;當我的軟件需要啓動軟件時,我想通過單一方法調用來完成,而不是通過代碼構建命令行並嘗試手動啓動軟件(就像它現在那樣)。

所以我發出一個請求幫助,指導和建議。任何你可以profer非常感謝。

P.S.我在這裏問這是因爲我像我經常那樣工作,作爲我公司的唯一開發人員;我沒有其他人將這些想法反彈出來。你們對這些東西有很多經驗,如果我不問你的建議,這將是愚蠢的,所以我希望你們都能忍受我。提前致謝!

回答

2

下面是這個重構的另一種方式:

using System.IO; 
public class ExternalApplication 
{ 
    public ExternalApplication(string path) 
    { 
     this.Path = path; 
    } 

    public string Path { get; protected set; } 

    public bool Exists() 
    { 
     if(string.IsNullOrEmpty(this.Path)) 
     throw new ConfigurationException("Path not specified."); 
     return File.Exists(this.Path); 
    } 

    public void Execute(string args) 
    { 
     // Implementation to launch the application 
    } 
} 

public class AppFactory 
{ 
    public ExternalApplication App1() 
    { 
     // Implementation to initialize this application from 
     // the application's configuration file. 
    } 

    public ExternalApplication App2() 
    { 
     // Implementation to initialize this application from 
     // the application's configuration file. 
    } 

    public ExternalApplication AppFromKey(string key) 
    { 
     // get from somewhere 
    } 
} 

在這種情況下,你有一個單一類型ExternalApplication和一個工廠,有方法的返回給你一個正確配置的應用程序。

0

對我來說似乎很合理。

我在過去做過類似的事情,但我沒有抽象基類。相反,我在構造函數中傳遞了應用程序路徑。

+0

我曾考慮過這樣做,但我們想要同步運行的一些應用程序以及其他異步運行的應用程序。因此需要重寫Execute。 – 2009-01-06 02:48:13

+0

如果我沒有預見到需要重寫Execute,我可以逃脫一個類,只是在某種配置初始化事件期間傳遞信息。但這樣,我得到了一種類型安全的Excel或PKZip應用程序對象。 – 2009-01-06 02:49:42

0

我不得不同意@Grauenwolf,這似乎是合理的。

根據通用性,您可能希望提供封裝配置檢索機制(一種拉出/設置命令行參數的方法)或如何執行應用程序(同步或異步)。

  1. 如果您發現某個已存在的環境,請嘗試在其周圍放置一個包裝並繼續。
  2. 添加這個額外的層沒有太多的努力,並有助於更好地分開應用程序的問題。我覺得它沒問題,並且可以幫助 通過嘲諷提高可測性。
  3. AppLauncherBase或AppExecutorBase? 沒有理由爲什麼應用程序需要是外部的。不是有史以來最好的名字,但我會嘗試封裝課程的目的,啓動/執行應用程序。
  4. 您可能需要考慮在App.Config中定義用於定義配置數據/應用程序路徑信息的約定,然後實現用於在基類中作爲默認值檢索它的邏輯。

祝你好運,我希望這有助於。