Dependency Injection in simple words

The concept of Dependency Injection can look over complicated especially for the beginners. Here I tried to explain it on a very simple example from the real world.

So:

  • Imagine you have a food delivery app in your mobile phone. Let's suppose it's a McDonald's app to be more precise.

  • So, you have a menu with a description of each item available there. These are your interfaces.

  • Then, you have the actual snacks which are delivered to you when you place an order. These are the implementions of your interfaces. Obviously, the implementations can differ depending from which particular restaurant they are be delivered.

  • Let's suppose that your app in some magical way has a zero-time delivery function (OK, almost zero-time). So, whatever place you are now (whatever part of your project), you select the items you need, press the "Order" button and the choosen snacks instantly appears in your hands. We can say that they are injected to the place where you are now :)

  • Moreover, if some snack (like breakfast or combo-meal) depends on other snacks, those dependand snacks are created automatically and also "injected" into your combo-meal. For example, Big Mac Combo Meal includes a BigMac burger, some fries and a drink. You don't need to order them separately. They all will be created and delivered to you automatically.

Resuming

Your food delivery app here is a Dependency Injection (DI) Container. The menu items there are interfaces of the services you might need somewhere in your project. The actual snacks are the implementations (actual classes). When you need your some snacks (services) you tell your app (DI container) what you need and they are delivered (injected in the place you are now) to you immediately.

Enjoy!

Subscribe

Subscribe to blog's updates. You will get notifications about new posts, and only them.

Recent posts

Supported by EasyQuery

EasyQuery is a multi-platform solution that lets you embedding ad-hoc reporting or advanced search functionality to your application