Inversion of Control and Dependency Injection


Published on

Published in: Technology
  • Be the first to comment

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

No notes for slide

Inversion of Control and Dependency Injection

  1. 1. Inversion of Control and Dependency Injection, StructureMap Andriy Buday
  2. 2. Loosely Coupled Systems Copyright © 2009 SoftServe, Inc.
  3. 3. A Concrete Example – A Music Player As a User of Music Player I want my Player to select songs of my favorite band from some list and play those songs… Copyright © 2009 SoftServe, Inc.
  4. 4. Music Player – The design <ul><li>Music Player is coupled to SongFinder – this is not good! </li></ul><ul><ul><li>Extensibility – what if not database but distributed cache </li></ul></ul><ul><ul><li>Testability – where do the songs for test come from? </li></ul></ul><ul><ul><li>Reusability – logic is fairly generic . . . </li></ul></ul>public class MusicPlayer { private SongFinder songFinder; public MusicPlayer() { songFinder = new SongFinder(); } public Song[] GetBandSongs(String band) { var allsongs = songFinder.FindAllSongs(); return allsongs .Where(x => x.Band == band).ToArray(); } } public class SongFinder { public virtual Song[] FindAllSongs() { var musStorage = new MusicStorage(); return musStorage.SONGS; } } Copyright © 2009 SoftServe, Inc.
  5. 5. Music Player – Introducing Interface <ul><li>Introduce interface/implementation separation </li></ul><ul><ul><li>Logic does not depend on SongFinder anymore. </li></ul></ul><ul><ul><li>Does this really solve the problem? </li></ul></ul>The constructor still has a static dependency on SongFinder public interface ISongFinder     {          Song[] FindAllSongs();     } private ISongFinder songFinder;       public MusicPlayer()       {           songFinder = new SongFinder();       } public Song[] GetBandSongs(String band) { var allsongs = songFinder.FindAllSongs(); return allsongs .Where(x => x.Band == band).ToArray(); } Copyright © 2009 SoftServe, Inc.
  6. 6. Music Player – Introducing Factory <ul><li>Introduce Factory </li></ul><ul><li>MusicPlayer decoupled from SongFinder </li></ul>    public class SongFinderFactory     {         public ISongFinder GiveUsDatabaseSongFinder()         {             return new SongFinder();         }     }      public MusicPlayer()     {        songFinder = SongFinderFactory.GiveUsDatabaseSongFinder();     } Copyright © 2009 SoftServe, Inc.
  7. 7. Another Idea Copyright © 2009 SoftServe, Inc. <ul><li>Why not just use the constructor? </li></ul>public class MusicPlayer { private ISongFinder m_songFinder; public MusicPlayer(ISongFinder songFinder) { m_songFinder = songFinder; } } <ul><li>One-shot initialisation – components are always initialised correctly </li></ul><ul><li>All dependencies are clearly visible from code </li></ul><ul><li>It is impossible to create cyclic dependencies </li></ul>This is Constructor Dependency Injection
  8. 8. Practice: ServiceLocator Copyright © 2009 SoftServe, Inc. <ul><li>Let us see code, we talked about </li></ul><ul><li>Let us implement something new and great </li></ul>
  9. 9. MusicPlayer – Introducing Service Locator <ul><li>Introduce ServiceLocator </li></ul><ul><li>This gives us extensibility, testability, reusability </li></ul>public static class ServiceLocator {     public static void RegisterService(Type type, Object impl)     { …     }     public static Object GetService(Type type)     { …     } } public MusicPlayer() {    var fiderService = ServiceLocator.GetService(typeof (ISongFinder));    songFinder = (ISongFinder)fiderService; } Copyright © 2009 SoftServe, Inc.
  10. 10. ServiceLocator - Problems Copyright © 2009 SoftServe, Inc. <ul><li>Sequence dependence </li></ul><ul><li>Cumbersome setup in tests </li></ul><ul><li>Code needs to handle lookup problems </li></ul>
  11. 11. What about Inversion of Control? Copyright © 2009 SoftServe, Inc. <ul><li>Dependency Injection - one example of IoC design principle </li></ul><ul><li>Also known as the Hollywood Principle </li></ul><ul><ul><li>Don’t call us, we’ll call you! </li></ul></ul><ul><li>Inversion of Control can make the difference between a library and a framework </li></ul>
  12. 12. IoC Containers Copyright © 2009 SoftServe, Inc. <ul><li>There are still some open questions </li></ul><ul><ul><li>Who creates the dependencies? </li></ul></ul><ul><ul><li>What if we need some initialisation code that must be run after dependencies have been set? </li></ul></ul><ul><ul><li>What happens when we don’t have all the components? </li></ul></ul><ul><li>IoC Containers solve these issues </li></ul><ul><ul><li>Have configuration – often external </li></ul></ul><ul><ul><li>Create objects </li></ul></ul><ul><ul><li>Ensure all dependencies are satisfied </li></ul></ul><ul><ul><li>Provide lifecycle support </li></ul></ul>
  13. 13. IoC Containers and Features Copyright © 2009 SoftServe, Inc.
  14. 14. Practice: StructureMap - from easy to advanced Copyright © 2009 SoftServe, Inc. <ul><li>Refactor MusicPlayer </li></ul><ul><li>Consumer and Soda example </li></ul><ul><ul><ul><li>config file configuration </li></ul></ul></ul><ul><ul><ul><li>fluent configuraion </li></ul></ul></ul><ul><li>Auto-wiring </li></ul>
  15. 15. Practice: StructureMap: Link project examples Copyright © 2009 SoftServe, Inc. <ul><li>As we use this on Link project… </li></ul>
  16. 16. Summary Copyright © 2009 SoftServe, Inc. <ul><li>Container based DI facilitates: </li></ul><ul><ul><li>Testability </li></ul></ul><ul><ul><li>Extensibility </li></ul></ul><ul><ul><li>Reusability </li></ul></ul><ul><li>Makes the difference between framework and library </li></ul><ul><ul><li>Not just use but extend </li></ul></ul><ul><li>Essential for complex Domain Driven Design </li></ul><ul><ul><li>Easier to separate 'infrastructure' from business logic </li></ul></ul><ul><li>We took a look on StructureMap IoC container </li></ul><ul><li>Also took a look on our product code usage of StructureMap </li></ul>
  17. 17. Questions? Copyright © 2009 SoftServe, Inc.
  18. 18. Some links: Copyright © 2009 SoftServe, Inc. <ul><li>My article: </li></ul><ul><li>http:// </li></ul><ul><li>StructureMap site: </li></ul><ul><li>http:// </li></ul><ul><li>MartinFlower article: </li></ul><ul><li>http:// </li></ul><ul><li>Derik Whittaker IoC dev movies: </li></ul><ul><li>http:// </li></ul>