↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Dependency Injection made simple

A page defines dependency injection as a simple way to deal with objects that depend on other objects. It first shows a coupled example in JavaScript and then replaces direct connection creation with injection in the constructor, to allow using different connections without changing the route.

Reading notes
#

  • Dependency injection is presented as the act of passing an object to the constructor or setter of a class that depends on it.
  • The initial example shows a posts route that directly creates a DatabaseConnection, which leaves the code coupled and not very flexible.
  • The alternative with SQLiteConnection and MySqlConnection passes the connection to the PostsRouter constructor, allowing the implementation used to be changed.
  • The text says that this approach decouples the objects and makes code management easier.
  • It also states that dependency injection helps with tests, because it is possible to pass a mock object to the post router.
  • In strongly typed languages, the text says that it is necessary to define a type for the object passed.
  • For this, the page introduces interfaces as a type structure without implementation of methods or properties.
  • In the C# example, the SQLiteConnection and MySqlConnection classes implement IDb.
  • PostsRouter receives IDb in the constructor and uses the connection through the interface.
  • The text ends with a complete example in C# using SQLiteConnection and MySqlConnection in two different PostsRouter objects.