Principles of dependency injection

  • Dependency injection can make your code more maintainable, using abstractions and by decoupling things.
  • If you code to implementation, you will get a tightly coupled and inflexible system. Use abstractions and make your code be reusable and flexible.
  • Follow the single responsibility principle and let each class do only one thing.
  • Differentiate between creatables (which should be created within the constructor if the whole class uses them) and injectables (which should be asked for by the constructor and not created directly).
  • Your constructors should only check for null, create creatables and store dependencies for later use. They should be free of any coding logic.

Full post here, 4 mins read