所以我重構了我繼承的遺留代碼庫,並且在此過程中,我找到了一個靜態類,它封裝了啓動第三方應用程序的邏輯。它本質上是這樣的(縮短爲簡潔起見,只顯示一個應用程序):.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.
}
}
我關注的是如下:
類,接口,或其他結構可能已經存在,做這個,我只是避風港」絆倒了它。
對於我想要做的事情,這可能是矯枉過正的。但是,請注意,該應用程序至少使用了三個獨立的第三方應用程序,這些應用程序目前爲止我們已經確定(並且更多幾乎肯定會彈出)。
我對基類的名字不太滿意。它看起來很模糊,而且信息量不大(但由於應用程序已經很好的定義,由框架保留,並且在我使用它的時候會產生嚴重的混亂),所以我想不出更好。
這個想法是我希望能夠在App.Config文件中保留應用程序配置數據(它的路徑和可執行文件名),並在我的應用程序啓動時檢查它的存在;當我的軟件需要啓動軟件時,我想通過單一方法調用來完成,而不是通過代碼構建命令行並嘗試手動啓動軟件(就像它現在那樣)。
所以我發出一個請求幫助,指導和建議。任何你可以profer非常感謝。
P.S.我在這裏問這是因爲我像我經常那樣工作,作爲我公司的唯一開發人員;我沒有其他人將這些想法反彈出來。你們對這些東西有很多經驗,如果我不問你的建議,這將是愚蠢的,所以我希望你們都能忍受我。提前致謝!
我曾考慮過這樣做,但我們想要同步運行的一些應用程序以及其他異步運行的應用程序。因此需要重寫Execute。 – 2009-01-06 02:48:13
如果我沒有預見到需要重寫Execute,我可以逃脫一個類,只是在某種配置初始化事件期間傳遞信息。但這樣,我得到了一種類型安全的Excel或PKZip應用程序對象。 – 2009-01-06 02:49:42