Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a decent way of injecting a dependency to a singleton class?

I have a singleton that depends on another class so I'd like to inject this dependency in order to make it unit-testable. Since there's no way to use constructor injection on the singleton I guess I should use setter injcetion instead but I really don't like it since at some point someone (probably myself) will forget to call the setter. You could of course inject the dependent objects to the singleton's getInstance method but this is pretty ugly as well. Are there some better ways of solving this (not using tools like IoC containers)?

public class Singleton {
    private ISomeDependency _dependency;
    private static final Singleton INSTANCE = new Singleton();
    private Singleton() {
    }
    public static Singleton getInstance() {
       return INSTANCE;
    }
    ...
}
like image 886
Christian Avatar asked Oct 24 '22 10:10

Christian


1 Answers

The singleton design pattern is a big anti-pattern, because there is no way to inject dependencies in a singleton. This doesn't mean however, that there is no way to have a single instance during the lifetime of the application, but you shouldn't use the singleton design pattern for this. Especially when using dependency injection, there is no reason to use this design pattern.

Just inject an instance of that type as constructor in the types that depend upon it and you're done. This gives you the possibility to build up the object graph in the top of your application and control the lifestyle there and inject dependencies into that instance.

The design of that class should just like any other class:

public class SomeService : ISomeService 
{
    private ISomeDependency _dependency;

    public ISomeService(ISomeDependency dependency)
    {
        _dependency = dependency;
    }

    // ISomeService members here
}

A dependency injection framework will allow you to define the lifestyle of any type very easily. For simple applications, you can however still do this by hand.

like image 170
Steven Avatar answered Oct 27 '22 09:10

Steven