Unity Singleton vs Static Class

Topics: Prism v4 - WPF 4
Mar 14, 2012 at 12:08 AM

Please comment on reasons to use Unity to register a type as a singleton over using a static class instead:

- Design Pattern Comments

- Testing Patterns

- Performance/Speed Data

Developer
Mar 14, 2012 at 6:16 PM

Hi,

Based on my understanding this choice will depend on the different requirements of your scenario.

In my opinion using the Unity Container might result profitable specially in cases where:

  • Classes have dependencies on other objects or classes
  • Dependencies are complex or require abstraction
  • Dependency injection might result handy

Additionally using Unity allows you to abstract from the implementation of your class as you will be able to resolve them through an interface. Allowing you to swap the implementation details of the class without affecting the components that depends on it.

Take into account that Dependency injection through Unity can have a minor impact on performance, and it can increase complexity where only simple dependencies exist. In those cases it could make sense to use static classes instead of a singleton in the container.

Also, you could find more information about this in the following links:

Regards,

Agustin Adami
http://blogs.southworks.net/aadami