What is a good analogy to understand IoC and DI?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you take the classic example of a Car. You could go through the regular car buying process and take the wheels the manufacturer gives you:
Then:
Or you could find a custom Car builder which allows you to specify exactly what kind of wheel you want your Car to use, as long as they conform to the specifications of a wheel:
Then:
But now you can inject whatever kind of wheel you want. Keep in mind that his is just one kind of Dependency Injection (constructor injection) but it serves as one of the easiest examples.