我在使用C#的内容管理系统中工作,并允许在中心类中添加单独的代码。出现的一个问题是我们希望为QA和网站的其余部分设置单独的代码库,目前我们使用文件夹结构将呼叫从一个类切换到另一个类
if (AssetPath == "Websites QA")
{
InputHelperQA.Navigation();//Calling Navigation Section From Helper Class
}
else
{
InputHelper.Navigation();
}
但我觉得这是完成这项任务的一种非常繁琐的方式。有没有更好的方法来实现这一点?显然只是添加InputHelper +“QA”不起作用,但是我们只需要调用方法一次而不必在调用周围包装if else的那些行。
答案 0 :(得分:1)
除了代表你的环境的分支外,你真的不应该为不同的环境设置单独的代码。
您确实应该将配置存储在配置文件或数据库中。
答案 1 :(得分:1)
你可能会做得更糟:
1)有一个界面(你可能已经拥有,真相被告知)
public interface IInputHelper
{
void Navigation();
}
2)按原样导出你的两个实例:
public class InputHelper : IInputHelper { }
public class InputHelperQA : IInputHelper { }
3)创建某种派遣经理:
public sealed class InputDispatch
{
private Dictionary<string, IInputHelper> dispatch_ = new Dictionary<string, IInputHelper>(StringComparer.OrdinalIgnoreCase);
public InputDispatch()
{
dispatch_["Websites QA"] = new InputDispatchQA();
dispatch_["Default"] = new InputDispatch();
}
public void Dispatch(string type)
{
Debug.Assert(dispatch_.ContainsKey(type));
dispatch_[type].Navigation();
}
}
答案 2 :(得分:1)
我会使用依赖注入。 StructureMap(仅作为一个示例)将允许您通过配置文件指定为接口提供的具体类型。