简体   繁体   English

处理两个不同的应用程序流

[英]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.

相关问题 处理应用程序中的不同用户类型 - Handling different user types in an application 在控制台应用程序中处理来自不同项目的依赖关系 - Handling a dependancy from different project in a console application 处理表单关闭事件的两种不同方式 - Two different ways of handling the form Close event 为什么在C#中处理相同的异常时会有两个不同的结果? - Why two different results while handling the same Exception in C#? 设计模式可容纳处理Winform函数的两种不同方式 - Design pattern to accommodate two different ways of handling a Winform's functions 两种不同的界面和一个应用程序的监视器 - Two different interfaces and monitors for one application 从两个不同位置安装ClickOnce应用程序 - Installing a ClickOnce application from two different locations 由两个不同的应用程序获得一次实例的类 - A class that are got instance once by two different application 是否可以从同一个应用程序登录到两个不同的 Application Insights 实例? - Is it possible to log to two different instances of Application Insights from the same Application? 在具有不同connectionStrings的两台不同服务器上部署同一Web应用程序? - Deploying same web application on two different servers with different connectionStrings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM