我目前有一个名为 ConfigProfile 工厂的类,它包含用于说默认配置文件、当前设置等的方法。我的个人资料服务在内部使用此类。我在想最好把它变成一个真正的工厂,然后为我们正在配置的每个产品创建适当的 Profile Service。
public string GetDefaultProfile(string product)
{
if (string.IsNullOrEmpty(product))
{
throw new ArgumentNullException("product");
}
string profile = null;
if (product.Contains("Product 1", StringComparison.CurrentCultureIgnoreCase) ||
product.Contains("product1", StringComparison.CurrentCultureIgnoreCase))
{
profile = Resources.product1DefaultProfile;
}
return profile;
}
那只是一种产品,但我们还有更多产品,这意味着我必须为每个产品添加更多 if 语句。配置文件服务已经有一个接口,并且是我的大部分程序使用的。还有几种方法使用这种相同的做事方式。那么根据产品名称返回适当配置文件服务的工厂会是更好的解决方案,还是我可以做其他事情?
编辑:这是此类中较简单的方法之一。更复杂的一种是从所需位置检索当前系统设置的一种。像所有产品一样都有 IIS 设置,但有些产品支持主题,而另一些产品则需要数据库配置。