Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perfecting Dependency Injection

Perfecting Dependency Injection

It’s in code bases all around the world, but are we getting all the value that the Dependency Injection pattern has to offer? Large dependency graphs, brittle test suites and hidden complexity have lead me to believe that we aren't. However, I think we can! In this talk we will cover when to use this pattern, how to use it and the pitfalls that we should be watching out for. We’ll also cover some alternatives to Dependency Injection, just in case you decide that it is not for you.

Matt Baker

May 11, 2017
Tweet

More Decks by Matt Baker

Other Decks in Programming

Transcript

  1. “Dependency Injection is a set of software design principles and

    patterns that enable us to develop loosely coupled code” - Mark Seemann @mattc_baker
  2. Some changes have been requested Log a message every time

    we save a new flight successfully Email customer every time we save a new flight successfully Email customer every time we fail to save a new flight @mattc_baker Publish a message every time we save a new flight successfully
  3. Handling changes with the Composition Root Add a timestamp to

    every log that is written @mattc_baker
  4. @mattc_baker Things to keep in mind - Don’t be a

    Control Freak - Favor Constructor Injection - Only inject volatile, or nondeterministic, dependencies - Don’t inject more than X dependencies - Solve Constructor Over-Injection with Facade Services - Avoid Service Location - Use Interception for your cross-cutting concerns - Use Interception for your cross-cutting concerns - If you choose to use Dependency Injection, use it everywhere in your application - Use a Composition Root - Use only one Composition Root - Keep your Composition Root close to your entry point
  5. “Dependency Injection is a set of software design principles and

    patterns that enable us to develop loosely coupled code” - Mark Seemann @mattc_baker