views:

80

answers:

2

What is difference between Singleton Scoping In Unity Framework and Singleton Pattern as general?

And with singleton pattern , you can not use mock object directly , you need to change some singleton code.

So is the singleton Anti-Pattern?

A: 

The singleton pattern is the pattern itself - Unity simply implements that pattern in several places (including most of the lifetime managers).

Singletons by definition cannot be mocked - they are designed to have only one instance. If your code that uses a singleton does by way of an interface then it is possible to mock a type to implement that interface but you cannot mock the singleton itself.

As for whether or not the singleton pattern is an anti-pattern I don't believe it is. I think that it is often overused and tends to be implemented in place of better solutions. I don't think that is a mark against the pattern itself, though.

Andrew Hare
"Unity simply implements that pattern in several places (including most of the lifetime managers.)" i am not agreeing with this statement. since you can have more than one injectors so you can get more than one instance of a class in a application.
saurabh
+1  A: 

A true singleton can only ever be instantiated once (per thread, in some implementations).

The Unity lifetime management is just an option to have Unity always return the same instance; that instance is unlikely to actually be designed as a singleton, so you could still, in principle, create other instances manually.

See my answer to this question for more about the distinction.

Jay