开发者

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 g开发者_StackOverflowuess 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;
    }
    ...
}


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.


You can use a dependency injection container (like Spring.Net, MS Unity, etc.) to get the concrete type of your dependency.

public class Singleton {
private ISomeDependency _dependency = Container.Resolve<ISomeDependency>();
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
   return INSTANCE;
}
...
}

Now you have the control outside of the singleton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜