I just watched an Inversion of Control course on Pluralsight. I get it now, but I’m writing this down to make sure I get it tomorrow.

  • Dependency Inversion Principle - accessed items should not depend on accessor items

  • Inversion of Control
    • a pattern to “solve” the Dependency Inversion Principle
    • accessor tells accessed how they will interact
    • Interface inversion - accessor creates interface rather than accessed
    • Flow inversion - event-driven instead of procedural programming
    • Creation inversion - object is created outside of the class it is used in
      • factory pattern (Button button = ButtonFactory.CreateButton();)
      • service locator (Button button = ServiceLocator.Create(IButton.class);)
      • dependency injection
  • Dependency Injection
    • dependency is created outside of class that depends on it
    • differs from creation inversion because it specifies that the dependency is not created by the dependent; in e.g. the factory pattern, the dependent is still responsible for creating the dependent by calling the factory

    • Constructor Injection
      • most common
      • dependency is passed into constructor and stored as field in object
    • Setter Injection
      • dependency is passed into setter and stored as field in object
    • Interface Injection
      • class has interface which requires a setter
      • when calling cast the class as the interface and call the interface’s setter
      • more explicit than just a setter
  • IoC Container
    • framework for doing dependency injection
    • resolves the chain of dependencies

They aren’t difficult concepts. Really, the most difficult part is keeping all the terminology straight.