Skip to content
Bdiebeak edited this page Jul 19, 2024 · 3 revisions

We all know what dependncies are and how difficult it can be to work with them. DI can help us to work with them.

Why do we need DI?
Because it helps us:

  • Move the creation of dependencies to another layer, adding the ability to manage the implementation, zone, and lifetime. This will allow us to separate the object creation code from the business logic and easily replace the dependency if necessary;
  • Correctly implement the program code with the entry points;
  • Get rid of the race conditions by separating initialization and start of our program;
  • Avoid implicit dependencies within the class (like static, singletons, etc.);
  • Automatically inject dependencies and simplify their creation;

You should remember a few simple rules:

  • A good function should get its dependencies through its arguments, and a good class should get its dependencies through its constructor;
  • The constructor should only include logic for initializing dependencies, not for performing any other tasks;
  • Dependencies should be based on abstractions, not on specific implementations (follow DIP);

This template uses Zenject as the dependency injectio framework, which is currently the industry standard.

Clone this wiki locally