Handling two different application flows

70 views Asked by At

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). 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

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. Any idea on how to tackle this.

Update: IDataManager Implementation

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

1

There are 1 answers

1
Mark Seemann On

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:

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? Wouldn't a bool suffice?