The problems with dependency injection and service locators solved

 For ant introduction to Dependency Injection and Service Locator, see the post in the bliki.

The problem with dependency injection frameworks are:

  1. With the frameworks: I don't want to annotate or anything. I want to write vanilla code
  2. There should not be one single place that has to know about every thing else. It is the ultimate violation about the open-closed-principle. Even if it is in main.
  3. Configuration explodes with dependencies of nested stuff. Main is on the top level and has no idea what some low level stuff should need
  4. Allowing only two presets (like dev and prod) sounds just stupid. 
  5. Tests should no have to configure anything they don't need, but should be able to configure anything they want
All in all dependency injections feels soooo heavy. Service Locators on the other hand - especially dynamic ones - are very easy to implement, understand and lightweight to use. But they have two major downsides:
  1. They hide dependencies
  2. You have to implement it in a way that test-isolation does not break (easier as the first point)

The best solution is as service-locator where every service specifies its dependencies (onto interfaces), which then are enforced by the compiler and visible for the external user. As external user I don't have to look it up and (hierarchical) presets are available when the service-locator is configured in an OCP-compatible manner. 

Stuff I want:
  • No hidden dependencies
  • No God-Configuration: Nobody has to configure stuff
  • Open for Extension, Closed against required Modification
  • Fully isolated in Tests
  • Flexible: If a user, like a test, want's to reconfigure its own system, it can and does not affect others
  • Hierarchical Presets: Sensible Presets definable, for example for def, test and prod
  • Components should be vanilla: No annotations, nothing to inherit
  • We should be able to make components ready externally, so that we can use third party modules
Regarding extends/implementation: Nothing > Optional Interface > Interface > Annotation > Parent > CodeGen. Where > means "is better than"

Comments