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?
-
2It'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. If you want to read about the difference between Unity and MEF, read the relevant section at msdn.microsoft.com/en-us/library/ff921140(v=PandP.40).aspx.– PatCommented Jan 6, 2011 at 18:37
-
1@Pat, MEF is a plug-in framework while Unity is a true DI container.– Grigori MelnikCommented Mar 15, 2013 at 0:17
3 Answers
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?
- the code is more maintainable because you don't rely on specific types
- the code is more testable
- the application is easily expandable
As a summary: it helps creating better applications faster.
-
2Does the service get Instanced when it's added to the Container or when the GetService method is called? If you ask for the same service in two different places but from the same Unity instance will you get the same object or seperate instances? Commented Feb 19, 2011 at 13:55
-
2I'm sorry, I can't tell you that, I never used Unity before. Generally spoken, it depends on the implementation of the container/framework. Have a look in the documentation, there ought to be a description of this. Also, some containers offer you the possibility to define a lifetime contract, so you can have singleton instances (always get the same) or new instances for each request. As said before, depends entirely on the container.– FemarefCommented Feb 19, 2011 at 14:00
-
Ok I've read parts of the Unity manual and this seems very configurable so I guess you could do it how you like. Commented Feb 20, 2011 at 0:53
-
@IngóVals As I am sure you have discovered by now Unity allows for a few variants of "the same service in two different places", one of which is a Singelton so you'd get the "the same object" and the other of which is per instance and would return "seperate instances". As usual, it depends on how you configure it. Commented Mar 21, 2012 at 21:07
-
1
Unity Container is like a jar full of cookies , when you need a cookie you just ask jar to give you a cookie.
Each cookie is having some virtues like you can have a cookie but you can't eat it because it is very hard to eat (something like singleton)
when your mom creates a new cookie , she just put that cookie in the jar rather than giving you directly!
-
26
-
3
-
I recommend you to watch Mike Taulty's Prism video series
The first two chapters will answer your question, and you can watch the other chapters to learn Prism (although its version 2 and quite old, the basic principles remains the same...)
Good luck :)