Dependency injection , Ninject and Unity containers seems like aliens name to junior developers including me few years back. Once a developer start learning design patterns, first few words he reads are loosely coupled, dependency, dependency inversion etc. – another aliens. bad joke! right? Jokes apart but learning design patterns specially DI is really a nightmare for developers.
So what can one do to understand these things? I am sharing here few things we could do to learn this. These I learn from my experience. If you go to this path, I am sure you will grab better idea of DI and containers and why should we use –
We do not understand any term and its practical use till we realize the need and importance of anything. Here are few steps to start. While writing I am following asp.net MVC framework. It is good to have a basic understanding of MVC framework to follow these steps –
1.Unit Test - To understand DI first start using unit testing. Suppose you are creating a MVC application. In controller you have object creation code of a class Product. Now you want to create unit text code for this controller action. How will you pass a sample mock object of Product class for test purpose?
But if our Controller is only depend on IProduct interface and does not directly depend on Product class! If we can directly inject a class object on demand, out controller will not be depend on class. DI and container(Ninject, Unity) do that for you.
2. Incorporate changes - Second thing you can do is , try to re engineer your code to incorporate infrastructure or requirement change. For example you have a third party class to process payment called in your controller code. Now there is a change request to replace payment class. What will you do? Change in every controller where this class object is created! Is this not difficult to incorporate change. there are other examples of change such functionality like logger, email sending etc.
But if we does not have this object initialization code in controller and someone from outside can create and pass this object into controller code? Nice! Yes dependency injection and container do the same. So in place of having dependency we inject or provide dependency from outside as per requirement.
3. Use different components for same service - If you want to use different components or services in different environment or platforms, you need to have your application to just configure the components in place of changing code for each environment.
With dependency injection and container, you can have this configuration in a class called Dependency resolver class.
4. Start from design principles - To study dependency injection, start from first design principle and grow one by one. Design principles gives you right start. by this you can learn about different aspects of good design.
How it works – You can have a quick idea how DI works from the image given in this article.
- Client code ask for an Interface.
- In Dependency Resolver class, Container is configured to provide object as per the interface requirement.
- Container create and return the object as per client need.
- In client we do not have any reference of Class but only interface.
if you are learning good design skills you need patience and a enthusiasm to deliver well maintainable software.
I have written whatever I have learn during my experience with DI and design patterns . Please give your suggestions at firstname.lastname@example.org . Thanks for reading!