【发布时间】:2009-01-06 02:32:00
【问题描述】:
所以我正在重构我继承的遗留代码库,在此过程中我发现了一个静态类,它封装了启动 3rd 方应用程序的逻辑。它基本上看起来像这样(为简洁起见,仅显示一个应用程序):
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.
}
}
我的担忧如下:
可能已经存在执行此操作的类、接口或其他构造,我只是没有偶然发现它。
对于我想做的事情来说,这可能有点矫枉过正。但是请注意,该应用程序至少使用了我迄今为止确定的三个独立的第 3 方应用程序(并且几乎肯定会弹出更多应用程序)。
我对基类的名称不太满意。它看起来很模糊,而且信息量不大(但我想不出比这更好的了,因为 Application 已经被很好地定义,由框架保留,如果我使用它会造成严重的混乱)。
我的想法是我希望能够将应用程序配置数据(它的路径和可执行文件名)保存在 App.Config 文件中,并在我的应用程序启动时检查它是否存在;当我的软件需要启动软件时,我想通过一个方法调用来完成,而不是使用代码构建命令行并尝试手动启动软件(就像目前一样)。
因此,我发送请求帮助、指导和建议。非常感谢您提供的任何内容。
附:我在这里问这个问题是因为我像往常一样在我的公司作为唯一的开发人员工作;我没有其他人可以反对这些想法。你们在这方面有很多经验,我不征求你们的意见是愚蠢的,所以我希望你们能容忍我。提前致谢!
【问题讨论】:
标签: c# .net oop refactoring