↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Dependency Injection in Elixir

The post explains dependency injection as passing objects or functions to another object or function, then shows how it can be used in Elixir through two approaches. It uses a birthday greetings example with Employees, GreetingMessage, and GreetingMessageSender to show why different collaborators may be swapped.

Reading notes
#

  • Dependency injection is presented as passing objects or functions to another object or function.
  • The birthday greetings example uses Employees, GreetingMessage, and GreetingMessageSender as separate collaborators.
  • One option is to read collaborators from Application.get_env/3, which makes it easy to switch configuration by environment.
  • The author notes that this approach can reduce visibility in tests because the collaborator lives in configuration files.
  • Another option is to pass collaborators as function parameters, making dependencies explicit.
  • With function parameters, tests keep collaborators in the same file and need fewer moving parts.
  • The downside of the parameter approach is that switching configuration between environments may be less quick.
  • The post ends by treating the choice as a trade-off and mentions macros, home-made implementations, and full-fledged frameworks as other possibilities.