Introduction to dependency injection

Today’s post will be more written, no code will be because today I will only explain what is dependency injection, why it is, what it is for, etc. in the next post will be the code.


Talk about Dependency Injection

As someone once said, “Each of us will sooner or later hear the word SOLID, some of our friends will say that our code should be SOLID”

Dependency Injection is very much related to the last principle of dependency inversion, which “book version” sounds like this:

  • High-level modules should not depend on low-level modules โ€“ the relationships between them should result from abstraction.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

Inversion of control is also a very important term related to Dependency Injection. Such my short explanatory rule inversion of control is “In the classic application, the programmer code calls functions from external libraries, while in the IoC approach this external framework calls the programmer’s code”

Okay, but what does Dependency injection have to do with these two concepts? Well, everything ๐Ÿ™‚ Dependency inversion explains how the code should look like, so that the dependencies of this code arise from abstraction and that they do not have to run masses of references without which they will not work.

Only Dependency Injection shows already ready solutions how to implement Dependency Inversion and Inversion of control in practice.

Okay, one by one ๐Ÿ™‚

  • Dependency inversion explains how to write loosely related classes, mainly to avoid new() in our classes because mainly the word new() does problems with dependencies, that you need to get some references later after some changes in the code :).
  • Inversion of control tells us how dependencies should depend on each other not to put in a class a specific type of another class only its interface, so that this class depends on abstraction.
  • And dependency injection tells us how to apply inversion of control, because let’s be serious ๐Ÿ™‚ ok, Interfaces are abstractions, but in some way we need to assign a specific class type to this interface ๐Ÿ™‚

If you know dependency Injection, then you will not have to worry that if you change something in some class, it will not cause error in another class which depends from this class, so it’s worth learning the dependency injection quickly when we have some of the dependencies from other classes.

And when we know Dependency Injection, we will be bosses:)

Another very big advantage of Dependency Injection is that when our code no longer drags a lot of dependencies behind it, it will be much easier to test, you will not have to play mocking all those classes that the tested class needs to run, because in the tested class there will be no references to classes with specific types, only their interfaces.

So using the advantages of Injection dependency we get a much more readable and flexible code and thus easier to maintain and test.

Later in examples with dependency injection I will use unit tests, but there is a lot of material on the web and when we say everything about dependency Injection, then there will probably be materials about unit testing ๐Ÿ™‚

And it is not true what the Lady below says ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

Maybe we will lose some time at the beginning, but later it will return it because we will not have to fix bugs every few moments.


Dependency injection frameworks

Such frameworks like Weld, Spring, Guice, Play framework, Salta, Glassfish HK2, Dagger support dependency injection, but are not necessary to implement dependency injection.



What does the dependency injection look like in diagram? Martin Fowler describes this as:

The MovieLister class depends on the MovieFinder class and its interface, so the MovieFinder class is injected into the MovieLister class, naturally the type of injected class will be its interface, so that this dependence comes from abstraction ๐Ÿ™‚



  • Dependency injection allows the client flexibility in configuring. Only customer behavior is fixed. The client can work in everything that supports the internal interface he expects.
  • Dependency Injection reduces the relationship between the class and its dependence.
  • Since Dependency injection does not require any changes to code behavior, this can be used to refactor the existing code. The result are customers which are more independent and easier to test in isolation using stubs and various dummy.
  • In dependency injection, the client does not need to know what exactly is in the code that he must use. Therefore, it helps to isolate the client from changes in the project which translates into greater flexibility, testability and ease of maintenance.
  • Dependency injection allows simultaneous or independent project development. This means that, for example, let’s assume four programmers can create independently of each other classes that will use each other and at the same time will only need to know the interfaces through which the classes will communicate.



  • Dependency injection forces you to complexity to get out of classes and links between classes that are not always easy to manage.
  • Dependency injection may addict on this scheme of injection dependencies and as we know excessive use of patterns causes a lot of problems ๐Ÿ™‚
  • Dependency injection requires the development of a well-thought-out operation of creating dependencies between classes, eg we have class A and interface B, but we do not have class C, which must use interface B in this case we have to think whether it all has arms and legs.
  • Dependency injection makes code more difficult to detect because it separates behavior from system design. This means that you need to track a larger number of files to monitor the system work.

Despite all the disadvantages that can be avoided in any case, if we do not overuse DI, dependency injection is good pattern to avoid a mass of dependencies.

Relax ๐Ÿ™‚ in the next article there will be examples with the code ๐Ÿ™‚



For now, we know why there is more or less dependency injection and to what it can be useful for in the next article we will inject dependencies on a specific example.

This content also you can find on medium:ย

As a standard, I remind you aboutย the newsletter, which I send notifications about new entries and additional information about the IT world in general.๐Ÿ™‚

Andย NECESSERILYย join theย DevmanCommunityย community on fb, part of the community is in one placeย ๐Ÿ™‚

โ€“ site on fb:ย Kowalski

โ€“ group on fb:ย DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want๐Ÿ™‚.

If that post was useful for you share it with your friends :)

Post a comment

Notify about