Dependency injection

1,427 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,427
On SlideShare
0
From Embeds
0
Number of Embeds
196
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Code example 1
  • Code example 2
  • Code example 3
  • Dependency injection

    1. 1. Dependency Injection (DI)<br />Or stop cooking spaghetti (code)<br />
    2. 2. 1<br /> “Dependency Injection <br /> is a key element <br /> of agile architecture”<br />Ward Cunningham<br />
    3. 3. Dependency Injection (DI) is not a pattern, but rather a set of tools<br />2<br />
    4. 4. DIisone of the ways to applyInversion of Control (IoC) principle<br />3<br />
    5. 5. Literally – let go off control and let someone feed you<br />4<br />
    6. 6. Example Task<br />Obviously its time to go to code . Check out NoDI project.<br />5<br />
    7. 7. Specifications are singing rigoletto<br />6<br />
    8. 8. Specs tend to change<br />7<br />
    9. 9. Highly coupled code is a step to spaghetti code<br />8<br />Can’t intercept and change code;<br />Can’t unit-test in isolation;<br />Conceptual problems in modules design;<br />Logic is spread across many parts.<br />
    10. 10. Divide et impera<br />9<br />DI’s base is Interface Oriented Programming. Write your code against abstractions rather then concrete implementations.<br />
    11. 11. Single Responsibility Principle (SRP)<br />10<br />By applying DI to class we let it “concentrate” on task it was designed for.<br />
    12. 12. Modularity<br /><ul><li>Replace;
    13. 13. Reuse;
    14. 14. Build in parallel;
    15. 15. Easier to maintain;
    16. 16. Easier to unit-test.</li></ul>Go on and check this stuff applied in PoorMansDI project.<br />11<br />
    17. 17. DI patterns.<br />12<br />
    18. 18. Composition Root<br />13<br />
    19. 19. Constructor injection<br />14<br />
    20. 20. Property (setter) injection<br />15<br />
    21. 21. Method injection<br />16<br />
    22. 22. Ambient context<br />17<br />
    23. 23. Which one to choose?<br />18<br />
    24. 24. 3D of DI<br />19<br />
    25. 25. 1. Composition<br />20<br />
    26. 26. 2. Lifetime Management<br />21<br /><ul><li>Transient (per call);
    27. 27. Singleton;
    28. 28. Per thread;
    29. 29. Per web request;
    30. 30. Per web session;
    31. 31. Pooled;</li></li></ul><li>3. Interception<br />22<br />
    32. 32. DI containers<br />23<br /><ul><li>Castle Windsor www.castleproject.org/container/index.html
    33. 33. StructureMaphttp://structuremap.github.com/structuremap/index.html
    34. 34. Spring.Nethttp://www.springframework.net/
    35. 35. PikoContainerhttp://www.picocontainer.org/
    36. 36. Ninjecthttp://ninject.org/
    37. 37. Unity http://unity.codeplex.com/
    38. 38. PocoCapsule/C++ http://www.pocomatic.com
    39. 39. etc.</li></li></ul><li>Ways to configure containers<br />See how to combine all of aforementioned styles in DIWithContainer project.<br />24<br />
    40. 40. Pros?<br />25<br />Simplified component architecture;<br />Easier to maintain;<br />Replace;<br />Reuse;<br />Build in parallel;<br />Easier to unit-test.<br />
    41. 41. Cons?<br />26<br />Have to learn something new?<br />Lower performance?(it depends)<br />It might be hard to fix lifetime errors.<br />
    42. 42. Should I use it?<br />27<br />Do you know OOP and design patterns?<br />Do you write unit-tests?<br />Do you use statically typed languages?<br />First learn it.<br />Don’t force a team that doesn’t see DI pros to use it.<br />
    43. 43. Thank you!<br />28<br />
    44. 44. Materials<br />30<br />M. Seemann. Dependency Injection in .Net - Manning Publications Co., 2010.<br />R. Osherove. The art of Unit Testing - Manning Publications Co., 2009, ISBN: 978-933988-27-6.<br />J. Miller. Cohesion And Coupling – MSDN Magazine, Oct. 2008. http://msdn.microsoft.com/en-us/magazine/cc947917.aspx<br />F. Maulo. GuyWire. http://fabiomaulo.blogspot.com/2009/11/guywire.html<br />K. Kozmic. How I use Inversion of Control containers. http://kozmic.pl/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx<br />

    ×