True Loosely Coupled Dependency Injection


Published on

Good practices and common misconceptions about Dependency Injection.

By Sergio Romero, Software Developer


Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

True Loosely Coupled Dependency Injection

  1. 1. True Loosely CoupledDependency Injection
  2. 2. About meSergio Romerosromero@teksystems.comBlog: http://sergeromerosoftware.wordpress.com2
  3. 3. Agenda• Inversion of Control• Dependency Injection• Anti-patterns• Patterns• Composition Root• Examples
  4. 4. Inversion of Control4
  5. 5. Dependency Injection5
  6. 6. Inversion of Control vs. DependencyInjection • Commonly used interchangeably as synonyms. • Dependency Injection is a way to implement Inversion of Control.6
  7. 7. Dependency Injection Benefits • Late binding • Extensibility • Maintainability • Parallel development • Testability7
  8. 8. Dependency Injection Anti-Patterns • Service Locator • Bastard Injection8
  9. 9. Service Locator • First described by Martin Fowler. • It is basically a static Dictionary that contains all the definitions of the objects to use:9
  10. 10. Why is it an anti-pattern? • Maintainability and reusability are hindered since: > Dependencies are hidden and could throw unexpected and hard to debug exceptions if the dependency is not properly configured. > All the assemblies now have a hard dependency on the service locator’s one. • Testability is hindered since configuration is needed for each testing assembly. • Not really necessary for DI to exist or work.10
  11. 11. Bastard Injection • Incorrectly confused with Poor Man’s DI. • Typically the overloaded constructor is used for testing and the default one for production code.11
  12. 12. Why is it an anti-pattern? • Use of foreign default: The class drags along the dependant assembly which may not be needed in another module. • The benefits of loose coupling are lost. The foreign default hinders the ability to reuse or replace the class and even the whole module. • It makes parallel development more difficult since now the class has a strong dependency. • A DI Container will not know which constructor to use.12
  13. 13. Dependency Injection Patterns • Constructor Injection • Property (Setter) Injection • Method Injection13
  14. 14. Constructor Injection • The Dependency is exposed in the class’ constructor. • Viewed in isolation makes it seem that the instantiation of the dependency is just pushed up one level.14
  15. 15. Property Injection• Should only be used when a good local default is available.15
  16. 16. Method Injection• Best used when implementation varies between calls to the method.16
  17. 17. The Composition Root • Where should we compose object graphs? As close as possible to the application’s entry point. • A (preferably) unique location in an application where modules are composed together. • Even a very modular application that uses loose coupling and late binding to compose itself has a root that contains the entry point into the application.17
  18. 18. The Composition Root • Examples of composition roots: > A console application is an executable (.exe) with a Main method. > An ASP.NET web application is a library (.dll) with an Application_Start event handler in its Global.asax. > A WPF application is an executable (.exe) with an App.xaml file. > A WCF service is a library (.dll) with a class that derives from a service interface, although you can hook into a more low-level entry point by creating a custom ServiceHostFactory.18
  19. 19. DI or IoC Containers • A DI Container is a software library that can automate many of the tasks involved in composing objects and managing their lifetimes. • Object composition may be achieved via Poor Man’s DI, but the challenges in managing the object’s lifetimes makes a DI Container an invaluable tool. • Most DI Containers will handle the most life cycles.19
  20. 20. DI or IoC Containers • Some of the most common DI Containers are: > Castle Windsor > Structure Map > Spring .Net > Autofac > Unity > MEF20
  21. 21. Composition Root with Poor Man’sDI Example21
  22. 22. Composition Root with IoCContainer Example22
  23. 23. References Injection in .Net, Mark Seeman, Manning Publications
  24. 24. Questions?