Member-only story
Dependency Injection — Overview and Implementation
Especially when you are at the beginning of your career as a software developer, you probably know that: You have already picked up the term “Dependency Injection”, but can’t do anything with it directly? Then today’s post is for you!
I will introduce you to the principle, functionality and the types of dependency injection. Finally, I’ll show you how to write a simple IoC container. And don’t worry if you are unfamiliar with the different terms, I will explain everything, you don’t need any previous knowledge. However, certain basic knowledge in software development is definitely an advantage today. My code examples are written in C#, but you will have no problems understanding them if you are used to another programming language.
What is Dependency Injection?
Dependency Injection (DI) is a term from the topic area “Inversion of Control” (IoC). IoC is a fundamental concept, DI is its implementation. An IoC container is a framework for dependency injection.
DI is the last point of Robert Martin’s SOLID principle. Martin defines that high-level modules should not depend on low-level modules, instead an abstraction level should be used. The easiest way to get a grasp on the concept is to look at an example: Let’s think of a program which processes input from the keyboard and sends it to a printer. This can be implemented by combining a class “printer” and “keyboard”. So far, so good. However, this approach becomes problematic if…