Do you inject your dependencies?

Updated by Brady Stroud [SSW] 1 year ago. See history

123

Injecting your dependency gives you:

  • Loosely coupled classes
  • Increased code reusing
  • Maintainable code
  • Testable methods
  • All dependencies are specified in one place
  • Class dependencies are clearly visible in the constructor inject Figure: Bad Example – A solution where each layer depends on static classes is not maintainable or testable inject Figure: Good Example – Dependencies in each layer should only be interfaces. This allows dependencies to be easily interchanged and unit tests to be written against mock/fake objects inject Figure: Bad Example – Classes should not include dependencies on database classes or business objects. Both of these classes may contain dependencies on external services like web services or databases inject Figure: Good Example – The dependencies are injected into the class. This enables alternative classes to be injected. For example, a DHLShippingCalculator should be easily substituted for a FedexShippingCalculator. A MockShippingCalculator and MockProductRepository could be injected if we wanted to run unit tests
acknowledgements
related rules