DEV Community

manojlingala
manojlingala

Posted on

Solving the Service Registration Dilemma with Service Resolver in Strategy Pattern

Strategy Pattern is a design pattern that provides a way to encapsulate algorithms, encapsulate methods that can be interchanged to solve a problem. One of the main advantages of this pattern is the ability to switch algorithms at runtime. This pattern involves creating objects that represent various strategies, and a context object whose behavior varies as per its strategy object.

When using strategy pattern, one issue faced is service registration, where multiple implementations of the same interface are registered with the dependency injection container. In such scenarios, the latest service registration overwrites previous configurations as they all have the same interface.

The Service Resolver pattern provides a solution to resolve the appropriate service instances when multiple implementations of the same interface are registered with the dependency injection container. It involves creating a Service Resolver class that holds a reference to the IServiceCollectionand uses the BuildServiceProvider() method of IServiceCollectionto resolve the appropriate service instance based on the provided product code.

Here is an example of Service Resolver implementation in C#.

public class ServiceResolver
{
    private readonly IServiceCollection _services;

    public ServiceResolver(IServiceCollection services)
    {
        _services = services;
    }

    public T Resolve<T>(ProductCode productCode)
    {
        switch (productCode)
        {
            case ProductCode.ProductA:
                return (T)_services.BuildServiceProvider().GetService(typeof(ProductAService));
            case ProductCode.ProductB:
                return (T)_services.BuildServiceProvider().GetService(typeof(ProductBService));
            default:
                throw new InvalidOperationException("Invalid product code");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The Service Resolver class can be registered in the Startup.cs file as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ServiceResolver>();
    services.AddScoped<IProductService,ProductAService>();
    services.AddScoped<IProductService,ProductBService>();
}
Enter fullscreen mode Exit fullscreen mode

Here is a sample XUnit test case for the ServiceResolver class:

public class ServiceResolverTests
{
    private ServiceResolver _serviceResolver;
    private IServiceCollection _services;
    private IServiceProvider _serviceProvider;

    [Fact]
    public void Resolve_ProductA_ReturnsProductAService()
    {
        // Arrange
        _services = new ServiceCollection();
        _services.AddScoped<IProductService, ProductAService>();
        _serviceProvider = _services.BuildServiceProvider();
        _serviceResolver = new ServiceResolver(_services);

        // Act
        var result = _serviceResolver.Resolve<IProductService>(ProductCode.ProductA);

        // Assert
        Assert.IsType<ProductAService>(result);
    }

    [Fact]
    public void Resolve_ProductB_ReturnsProductBService()
    {
        // Arrange
        _services = new ServiceCollection();
        _services.AddScoped<IProductService, ProductBService>();
        _serviceProvider = _services.BuildServiceProvider();
        _serviceResolver = new ServiceResolver(_services);

        // Act
        var result = _serviceResolver.Resolve<IProductService>(ProductCode.ProductB);

        // Assert
        Assert.IsType<ProductBService>(result);
    }

    [Fact]
    public void Resolve_InvalidProductCode_ThrowsInvalidOperationException()
    {
        // Arrange
        _services = new ServiceCollection();
        _services.AddScoped<IProductService, ProductAService>();
        _serviceProvider = _services.BuildServiceProvider();
        _serviceResolver = new ServiceResolver(_services);

        // Act and Assert
        Assert.Throws<InvalidOperationException>(() => _serviceResolver.Resolve<IProductService>((ProductCode)99));
    }
}
Enter fullscreen mode Exit fullscreen mode

In conclusion, the Strategy Pattern is a powerful design pattern that provides a flexible way of switching between different implementations of a single interface. However, when it comes to service registration, this pattern can cause some problems, such as overwriting previous configurations, and making it difficult to determine the correct service to use. The Service Resolver is a solution to this problem, allowing for the resolution of the correct service instance to be performed dynamically at runtime. This can be achieved through the use of the switch statement, which enables the selection of the appropriate service instance based on the input received.

In this article, we have covered the basics of the Strategy Pattern, the issues it can cause in service registration, and how the Service Resolver can be used to resolve these problems. Furthermore, we have demonstrated an example implementation of the Service Resolver and its accompanying unit tests, providing a solid foundation for anyone looking to incorporate this pattern into their own projects.

Top comments (0)