我刚刚开始使用 IoC 容器,如果这是一个愚蠢的问题,我深表歉意。
我在应用程序中有如下代码
internal static class StaticDataHandlerFactory
{
public static IStaticDataHandler CreateHandler(StaticDataUpdate staticDataUpdate)
{
if (staticDataUpdate.Item is StaticDataUpdateOffice)
{
return new OfficeUpdateHandler();
}
if (staticDataUpdate.Item is StaticDataUpdateEmployee)
{
return new EmployeeUpdateHandler();
}
if (staticDataUpdate.Item == null)
{
throw new NotImplementedException(
string.Format("No static data provided"));
}
else
{
throw new NotImplementedException(
string.Format("Unimplemented static data type of {0}", staticDataUpdate.Item.GetType().FullName));
}
}
}
它基本上是一个简单的工厂,它返回处理输入数据的正确策略。
IoC 容器会允许我消除这样的代码吗?也就是说:它是否允许我根据输入参数的类型动态选择要加载的具体实现?
还是我在这里偏离了方向?