Dependency Injection & IoC


Published on

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Dependency Injection & IoC

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