Dependency Injection & Inversion of Control Unity Application Block vs. Spring.NET Dennis Loktionov [email_address] February 2009
Agenda What is a Dependency? Dependency Injection Pattern Injection Types Dependency Injection Pros & Cons Spring.NET vs. MS Unity Application Block References Questions
What is a Dependency?   Common Dependencies Application Layers --  Data Access Layer --  Business Layer --  Presentation Layer External Services  -- Web Services --  WCF Services External Components --  Frameworks & Libraries --  3 rd  Party Components
Application Layers Dependencies
Why dependencies are evil?   Problems that dependencies create Tight coupling of software components Software becomes hard to maintain Difficult to isolate when testing
Dependency Injection Pattern WHAT Dependency Injection (DI) in computer programming refers to the process of supplying an external dependency to a software component or service. WHY Ability to specify component dependencies at run time Simplified management of cross-cutting concerns Increased  testability of individual components Need for pluggable architecture
Dependency Injection Types Types of Dependency Injection Constructor Injection Property (Setter) Injection Method Call Injection
Dependency Injection Types Constructor Injection
Dependency Injection Types Property (Setter) Injection
Dependency Injection Types Method Call Injection
Dependency Injection Pros & Cons Pros Loosely Coupled & Highly Cohesive Increased Testability & Maintainability Flexibility of run time configuration Inversion of Control containers do the wiring for you Cons Increased code complexity Can complicate debugging during the learning curve Code flow becomes not trivial
Inversion of Control Dependency Inversion Principle (DIP) The principle states that high level or low level modules should not depend upon each other, instead they should depend upon abstractions. Specific implementation (object instances) are deferred to a higher level of abstraction control. Parent Classes Container
Frameworks Frameworks Spring.NET MS Unity Application Block Castle Windsor StructureMap Ninject PicoContainer.NET …  and many more
Spring.NET Spring.NET Architecture
Spring.NET Inversion of Control Container in Action
Spring.NET: code samples Movie Lister example
MS Unity Application Block Unity Application Block Architecture
Unity: code samples Movie Lister example Quick start web example
Dependency Injection Pitfalls Circular References Objects generated though constructor injection that reference each other in their constructor parameters Objects generated though constructor injection that  pass a reference of their type in constructor Objects generated though method call injection that reference each other Objects generated though property injection that reference each other Partial Trust Environment Limitations
References Useful links: http://martinfowler.com/articles/injection.html http://www.springframework.net/ http://www.codeplex.com/unity
THANKS
Questions ? Dennis Loktionov [email_address] 604-628-5100 ext .356

Dependency Injection & IoC

  • 1.
    Dependency Injection &Inversion of Control Unity Application Block vs. Spring.NET Dennis Loktionov [email_address] February 2009
  • 2.
    Agenda What isa Dependency? Dependency Injection Pattern Injection Types Dependency Injection Pros & Cons Spring.NET vs. MS Unity Application Block References Questions
  • 3.
    What is aDependency? Common Dependencies Application Layers -- Data Access Layer -- Business Layer -- Presentation Layer External Services -- Web Services -- WCF Services External Components -- Frameworks & Libraries -- 3 rd Party Components
  • 4.
  • 5.
    Why dependencies areevil? Problems that dependencies create Tight coupling of software components Software becomes hard to maintain Difficult to isolate when testing
  • 6.
    Dependency Injection PatternWHAT Dependency Injection (DI) in computer programming refers to the process of supplying an external dependency to a software component or service. WHY Ability to specify component dependencies at run time Simplified management of cross-cutting concerns Increased testability of individual components Need for pluggable architecture
  • 7.
    Dependency Injection TypesTypes of Dependency Injection Constructor Injection Property (Setter) Injection Method Call Injection
  • 8.
    Dependency Injection TypesConstructor Injection
  • 9.
    Dependency Injection TypesProperty (Setter) Injection
  • 10.
    Dependency Injection TypesMethod Call Injection
  • 11.
    Dependency Injection Pros& Cons Pros Loosely Coupled & Highly Cohesive Increased Testability & Maintainability Flexibility of run time configuration Inversion of Control containers do the wiring for you Cons Increased code complexity Can complicate debugging during the learning curve Code flow becomes not trivial
  • 12.
    Inversion of ControlDependency Inversion Principle (DIP) The principle states that high level or low level modules should not depend upon each other, instead they should depend upon abstractions. Specific implementation (object instances) are deferred to a higher level of abstraction control. Parent Classes Container
  • 13.
    Frameworks Frameworks Spring.NETMS Unity Application Block Castle Windsor StructureMap Ninject PicoContainer.NET … and many more
  • 14.
  • 15.
    Spring.NET Inversion ofControl Container in Action
  • 16.
    Spring.NET: code samplesMovie Lister example
  • 17.
    MS Unity ApplicationBlock Unity Application Block Architecture
  • 18.
    Unity: code samplesMovie Lister example Quick start web example
  • 19.
    Dependency Injection PitfallsCircular References Objects generated though constructor injection that reference each other in their constructor parameters Objects generated though constructor injection that pass a reference of their type in constructor Objects generated though method call injection that reference each other Objects generated though property injection that reference each other Partial Trust Environment Limitations
  • 20.
    References Useful links:http://martinfowler.com/articles/injection.html http://www.springframework.net/ http://www.codeplex.com/unity
  • 21.
  • 22.
    Questions ? DennisLoktionov [email_address] 604-628-5100 ext .356