[英]Handling two different application flows
We are building an application where we have to have both old and new version to work side by side (V1 is old and V2 is new). 我们正在构建一个应用程序,其中必须同时具有旧版本和新版本才能同时工作(V1是旧版本,V2是新版本)。 Now to handle new flow we are using same old interfaces with everything being the same and differs only in functionality, hence now we have to define a named instance in-order to resolve the instances for new flow.
现在,为了处理新流程,我们使用相同的旧接口,并且所有功能都相同并且仅在功能上有所不同,因此,现在我们必须定义一个命名实例,以便为新流程解析实例。
In the process teams have started using Service Factory Pattern as shown below 在此过程中,团队已开始使用Service Factory Pattern,如下所示
class DataProcessor
{
private readonly IDataManager _dataManager;
public DataProcessor(IServiceFactory serviceFactory)
{
_dataManager = serviceFactory.GetInstance<IDataManager>();
}
public void Execute()
{
_dataManager.Run();
}
}
Service Factory Class 服务工厂类
public class ServiceFactory : IServiceFactory
{
private readonly IFeatureEvaluator _featureEvaluator;
public ServiceFactory(IFeatureEvaluator featureEvaluator)
{
_featureEvaluator = featureEvaluator;
}
public T GetInstance<T>()
{
if (_featureEvaluator.IsEnabled<"V2">())
{
return ObjectFactory.GetInstance<T>("V2")
}
return ObjectFactory.GetInstance<T>();
}
}
Since Service Factory is anti-pattern and also it creates lot of complexities in retiring the old flow in future, I would want a way to initialize the dependencies at the container(structuremap ioc) itself or to work in a "Pure DI" way so that we can avoid headache. 由于Service Factory是反模式的,并且在将来退出旧流程时也会造成很多复杂性,因此,我需要一种方法来初始化容器本身的依赖关系(structuremap ioc)或以“纯DI”方式工作,因此我们可以避免头痛。 Any idea on how to tackle this.
关于如何解决这个问题的任何想法。
Update: IDataManager Implementation 更新:IDataManager实施
public interface IDataManager
{
void Run();
}
public class OldFlow : IDataManager
{
public void Run()
{
//
}
}
public class NewFlow : IDataManager
{
public void Run()
{
//
}
}
IDataManager has 2 implementations and resolving the instance should be based on _featureEvaluator, if V2 flow then "newflow" should be instantiated else "old flow" instance IDataManager有2种实现,解析实例应基于_featureEvaluator,如果V2流,则应实例化“ newflow”,否则应实例化“旧流”实例
Why don't you just inject the dependency you need? 您为什么不只注入所需的依赖项?
public class DataProcessor
{
private readonly IDataManager _dataManager;
public DataProcessor(IDataManager dataManager)
{
_dataManager = dataManager;
}
public void Execute()
{
_dataManager.Run();
}
}
In your Composition Root you can conditionally compose DataProcessor
with the implementation of IDataManager
you'd like: 在您的“ 合成根”中,您可以有条件地将
DataProcessor
与您想要的IDataManager
的实现组成:
public DataProcessor CreateDataProcessor()
{
if (_featureEvaluator.IsEnabled<"V2">())
{
IDataManager dm = new NewFlow();
return new DataProcessor(dm);
}
IDataManager dm = new OldFlow();
return new DataProcessor(dm);
}
This seems to be similar to feature toggles. 这似乎类似于功能切换。 Why, by the way, is
_featureEvaluator
an interface? 顺便说一下,为什么
_featureEvaluator
是接口? Wouldn't a bool
suffice? bool
不够吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.