Use dependency injection in static class

You basically have two options:

  1. Change the class from static to an instance class and supply the dependency through Constructor Injection.
  2. Supply the dependency to the class’s public method through Method Injection.

Here are examples for each option.

Option 1. Change the class from static to an instance class

Change the class from static to an instance class and supply IConfiguracion through Constructor Injection. XHelper should in that case be injected into the constructor of its consumers. Example:

public class XHelper
{
    private readonly IConfiguration config;
    
    public XHelper(IConfiguration config)
    {
        this.config = config ?? throw new ArgumentNullException("config");
    }

    public TResponse Execute(string metodo, TRequest request)
    {
        string y = this.config.apiUrl;

        return xxx;
    }
}

2. Supply the IConfiguration to the Execute method through Method Injection.

Example:

public static class XHelper
{
    public static TResponse Execute(
        string metodo, TRequest request, IConfiguration config)
    {
        if (config is null) throw new ArgumentNullException("config");
    
        string y = config.apiUrl;

        return xxx;
    }
}

Less favorable options

There are of course more options to consider, but I consider them all to be less favorable, because they would either cause code smells or anti-patterns.

For instance:

  • You might be inclined to make the DI Container accessible through a static method and call it from inside your static class, but this is an anti-pattern called Service Locator.
  • You could allow setting the static class’s dependencies through static properties on that class, but this leads to the Ambient Context anti-pattern.
  • You could change the class to an instance class, but instead of using Constructor Injection make use of Property Injection, but this causes Temporal Coupling, which is a code smell.

Leave a Comment