Can someone please explain to me the notion of a Unity Container like I'm a 6 year old kid? How does it work and what does it do?
It's not so much that there is a Unity container, but 'Unity' is the name of a particular Dependency Injection (DI) container. Prism also comes with MEF and supports any other DI container. Unity just came first.
The Unity Container (Unity) is a full featured, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: Simplified object creation, especially for hierarchical object structures and dependencies.
Unity container is an open source IoC container for . NET applications supported by Microsoft. It is a lightweight and extensible IoC container. The source code for Unity container is available at https://github.com/unitycontainer/unity.
Unity Container is a full featured, general-purpose IoC container for use in any type of . NET application. It is Open Source and released under Apache 2.0 license. Unity is extensible. Anyone can write an extensions that changes the behavior of the container, or adds new capabilities.
This is a more technical description of the background, I hope you still find it useful.
Generally put, it is a DI (dependency injection) container.
Given the following class:
public class Sample { Service a; public Sample() { a = new Service(); } }
The problem with that is that it initializes its own version of Service
, making it very hard to adjust for code changes (ie. if you want to exchange Service
with something different). Also it makes testing difficult.
To resolve that, don't actually create it yourself, but get it from the outside:
public class Sample { Service a; public Sample(Service aService) { a = aService; } }
Now you have taken the creation away from the class you can just put it in there from the outside, increasing testability and maintainability. However, you still have a dependency on the class Service
. You aren't really interested in that specific class, but in the behaviour it offers - so you make in interface out of it.
public class Sample { IService a; public Sample(IService aService) { a = aService; } }
Now, you can replace the service with anything you like. For example, you have a class getting data from a server using a service. Now, you want to test only the data parsing and not the data fetching service - just create a class implementing the interface, serving static data - done!
Now, Unity comes into play. At the moment, you have to resolve the dependencies yourself. What unity does is simple - it takes all classes that have dependendencies and resolves those - so you can just call (pseudocode, I don't know unity):
UnityContainer uc = new UnityContainer(); var a = uc.GetService<IService>();
And it gets you the readily useable class.
What do we have achivied by that?
As a summary: it helps creating better applications faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With