aelf Tech Talks: Dependency Injection Part 1

Injection & Inversion of Control (IoC)

aelf Developer
aelf
4 min readSep 19, 2019

--

by Yiqi Zhao

#What is dependency injection?

Within the developer community, we have a concept called “dependency”. When a program needs to use a third-party library, we can refer to this library as a dependency. But where does “injection” come in?

There is a saying, that to find a person’s worth you need to look at his opponents. The opponent of dependency injection is dependency lookup. Dependency injection and dependency lookup are both ways to implement Inversion of Control (IoC).

The main goal of Inversion of control and Dependency Injection is to remove dependencies of an application. This makes the system more decoupled and maintainable.

Inversion of Control

First let’s try to understand IOC (Inversion of control). If you go back to old computer programming days, program flow used to run in its own control. For instance let’s consider a simple chat application flow as shown in the below flow diagram.

  1. End user sends chat message.
  2. Application waits for the message from the other end.
  3. If no message is found it goes to Step 2 or else moves to Step 4.
  4. Displays the message.
  5. User continues with his work ahead.

Now if you analyze the program flow closely, it’s sequential. The program is in control of himself. Inversion of control means the program delegates control to someone else who will drive the flow. For instance if we make the chat application event based then the flow of the program will go something as below:-

  1. End user sends chat message.
  2. User continues with his work ahead.
  3. Application listens to events. If a message arrives event is activated and message is received and displayed.

If you see the program flow it’s not sequential, its event based. So now the control is inverted. So rather than the internal program controlling the flow, events drive the program flow. Event flow approach is more flexible as their no direct invocation which leads to more flexibility.

A word of caution here, do not conclude that IOC are implemented by only events. You can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways.

IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC. IOC is a concept where the flow of application is inverted. So for example rather than the caller calling the method.

The two methods mentioned earlier, dependency injection and dependency lookup, are methods that you can use anywhere.

You may have heard of the service locator in the spring framework. Now there are many predecessors who think that Service Locator is an anti-pattern. In fact, service Locator is a concrete implementation of dependency lookup. Although the idea of service locator and dependency injection are essentially opposites, MS.DI uses the service locator when implementing the dependency injection framework.

If you haven’t used spring, it’s okay.

Let us try to clarify the relationship between the two. Obviously, Dependency Injection is mainly about injecting. There is a very interesting answer on Dependency Injection on Stack Overflow. The question is how to explain dependency injection to a five-year-old child:

When a child goes and gets things out of the refrigerator by themselves, they might create problems. They might leave the door open, or might get something Mommy or Daddy doesn’t want them to have. They might even be looking for something they don’t even have or which has expired.

What they should be doing is stating a need, “I need something to drink with lunch,” and then the parent will make sure they have something when they sit down to eat.

So the right approach is to ask the children what they need, then we will find something from the refrigerator and find out and hand it to them.

To make an analogy, children taking out things from the refrigerator is similar to dependency lookup; when we provide things to children as adults, this is dependency injection.

Before discussing dependency injection in detail, I think we need to review the five principles of design patterns (some places mention six or even seven, but these overlap the basic five), Part 2 in this series will go through the first couple of these principles

— Join the Community:

· Get on our Telegram Discord Slack and Kakao channel

· Follow us on Twitter Reddit and Facebook

· Read weekly articles on the aelf blog

· Catch up with the develop progress on Github

· Telegram community in 한국 ,日本語 ,русский ,العربية ,Deutsch ,ItalianoandTiếng Việt,

· Instagram: aelfblockchain

· YouTube Channel: aelf

For more information, visit aelf.io

--

--