Today, I got tasks which relate to lots of classes which are designed using many modern design patterns. While trying to understand the codes of the team project, I saw “container” appearing almost everywhen the code creates an instance object. I didn’t get or guess the idea behind those design… I got home in a very frustrating mind about the “container”!
Got home, I started to search about the unity container. Not so bad, finally I have understood it. And here now I sit back and write these lines to tell you about this pattern. I wonder why people trying to talk about it so mysteriously! When you got what they are trying to say, you see it very simple. Here is how I got the idea from Unity – Dependency Injection and Inversion of Control Container.
The context is that you have written a program using a bundle of classes. Problems occur when you wanna update your classes or even worse when you wanna change/replace some of your classes. In that case, it would be so nice if you would change/update/replace easily by updating just dozen of lines of code! Unity Container is what you need right then!
Using Unity Container, you create the object/instance of your class using a container. The class you use is an interface only. The concrete class is loaded from configuration file by the container. You can also load your class at run-time if you want to. So, later on, whenever you wanna update/change the concrete class, just go for the configuration file!
Moreover, when initing an instance, you may have arguments for its constructor which are again objects of other classes. The nice thing here is that the container will take care about loading those stuff for you as long as you register them (in the configuration of course)!
And more, you can have a property whose type is a class. You can have it loaded by the container too (using property [Dependent]).
Yeah, that’s what I feel great about Unity Container. Without understanding it, the codes are nightmare to me! Got it now and the codes are just… puppies ^_^