Dependency Injection<br />And using an Inversion of Control (IoC) container<br />
What is Dependency Injection<br />Giving an object instance it’s variables<br />
Dependency Non-Injection<br />public class Whatever<br />{<br />  private DbStuff _db;   This is a variable<br />  public...
And this is bad for all kinds of reasons… <br />
Single Responsibility Principle<br />A class should have one, and only one, reason to change.<br />We switch from SQL Serv...
Dependency Injection<br />public class Whatever<br />{<br />  private DbStuff_db;<br />  public Whatever(DbStuff database)...
Win<br />Our Whateverclass still depends on DbDatabut it is not responsible for it<br />By adhering to SRP through depende...
But wait! We still have a problem<br />
Dependency Inversion Principle<br />Depend on abstractions, not on concretions.<br />We can improve the situation by depen...
Dependency Injection v 2.0<br />public class Whatever<br />{<br />  private IDbStuff _db;    Better<br />  public Whateve...
To summarize, DI is about passing instance variables<br />
Benefits<br />High cohesion because classes are focused on doing one thing really well<br />Testability<br />IDbStuff data...
private readonlySystemConfiguration _configuration;<br />private readonlyIWorkorderEntityBuilder _entityBuilder;<br />publ...
Inversion of Control Containers<br />(IoC is a terrible term but we’re stuck with it. But maybe we’ll use container instea...
Manage Dependencies<br />DI makes for cohesive, loosely couple software<br />But also more moving parts<br />IoC container...
ContainersA Short List<br />Spring<br />Spring.NET<br />Pico<br />Castle Windsor<br />StructureMap<br />Ninject<br />Autof...
Workflow<br />
ForRequestedType&lt;ISpatialQuery&gt;()                .TheDefaultIsConcreteType&lt;SpatialQueryGenericClient&gt;();<br />
ForRequestedType&lt;ISettingRepository&gt;()                .TheDefault.Is.OfConcreteType&lt;ApplicationSettingRepository&...
var settings = ObjectFactory.GetInstance&lt;ISettingRepository&gt;();<br />
Autowiring Is A Big Win<br />
Things depend on things depend on things…<br />varprocessor = ObjectFactory.GetInstance&lt;IMessageProcessor&gt;();<br />
Features<br />Many other features but that’s the core<br />Manage lifetime of objects<br />Singleton, per-thread, per-sess...
Upcoming SlideShare
Loading in...5
×

Dev Cast Dependency Injection

1,168

Published on

A very quick overview of dependency injection and IoC. For an internal DevCast meeting

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,168
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
37
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Dev Cast Dependency Injection

  1. 1. Dependency Injection<br />And using an Inversion of Control (IoC) container<br />
  2. 2. What is Dependency Injection<br />Giving an object instance it’s variables<br />
  3. 3. Dependency Non-Injection<br />public class Whatever<br />{<br /> private DbStuff _db;  This is a variable<br /> public Whatever()<br /> {<br /> _db = new DbStuff();  And Whatever<br /> } depends on it<br />so it creates it<br /> public void DoIt()<br /> {<br /> _db.GetData();<br /> }<br />} <br />
  4. 4. And this is bad for all kinds of reasons… <br />
  5. 5. Single Responsibility Principle<br />A class should have one, and only one, reason to change.<br />We switch from SQL Server to Oracle and our Whatever class needs to change<br />
  6. 6. Dependency Injection<br />public class Whatever<br />{<br /> private DbStuff_db;<br /> public Whatever(DbStuff database)<br /> {<br /> _db = database;  The ‘new’ keyword<br /> } is outta there<br /> public void DoIt()<br /> {<br /> _db.GetData();<br /> }<br />} <br />
  7. 7. Win<br />Our Whateverclass still depends on DbDatabut it is not responsible for it<br />By adhering to SRP through dependency injection we increasecohesion<br />
  8. 8. But wait! We still have a problem<br />
  9. 9. Dependency Inversion Principle<br />Depend on abstractions, not on concretions.<br />We can improve the situation by depending on an interface or abstract class<br />
  10. 10. Dependency Injection v 2.0<br />public class Whatever<br />{<br /> private IDbStuff _db;  Better<br /> public Whatever(IDbStuff database)<br /> {<br /> _db = database;  life is good<br /> }<br /> public void DoIt()<br /> {<br /> _db.GetData();<br /> }<br />} <br />
  11. 11. To summarize, DI is about passing instance variables<br />
  12. 12. Benefits<br />High cohesion because classes are focused on doing one thing really well<br />Testability<br />IDbStuff data = new FakeDbWithBadData();<br />var test = new Whatever(data);<br />Assert.Throws(Exception,test.DoIt());<br />The class under test did not have to change. Magic!<br />
  13. 13. private readonlySystemConfiguration _configuration;<br />private readonlyIWorkorderEntityBuilder _entityBuilder;<br />public NewEntityWorkorderManager(IWorkorderRepositoryworkorderRepository,<br />IMessageRepositorymessageRepository,<br />IWorkorderEntityBuilderentityBuilder,<br />SystemConfiguration configuration,<br />ICityworksRepositorycityworksRepository)<br />: base(workorderRepository, messageRepository,<br />cityworksRepository, configuration)<br /> {<br /> _entityBuilder = entityBuilder;<br /> _configuration = configuration;<br /> }<br />
  14. 14. Inversion of Control Containers<br />(IoC is a terrible term but we’re stuck with it. But maybe we’ll use container instead. At least it’s shorter.)<br />
  15. 15. Manage Dependencies<br />DI makes for cohesive, loosely couple software<br />But also more moving parts<br />IoC containers exist to make dependency injection easier and more predictable<br />
  16. 16. ContainersA Short List<br />Spring<br />Spring.NET<br />Pico<br />Castle Windsor<br />StructureMap<br />Ninject<br />Autofac<br />Unity<br />Glassfish and any EJB 3.0 app server<br />
  17. 17. Workflow<br />
  18. 18. ForRequestedType&lt;ISpatialQuery&gt;() .TheDefaultIsConcreteType&lt;SpatialQueryGenericClient&gt;();<br />
  19. 19. ForRequestedType&lt;ISettingRepository&gt;() .TheDefault.Is.OfConcreteType&lt;ApplicationSettingRepository&gt;() .CtorDependency&lt;IUnitOfWork&gt;(&quot;unitOfWork&quot;) .Is(u =&gt; u.TheInstanceNamed(Resources.WCSDatasource));<br />
  20. 20. var settings = ObjectFactory.GetInstance&lt;ISettingRepository&gt;();<br />
  21. 21. Autowiring Is A Big Win<br />
  22. 22. Things depend on things depend on things…<br />varprocessor = ObjectFactory.GetInstance&lt;IMessageProcessor&gt;();<br />
  23. 23. Features<br />Many other features but that’s the core<br />Manage lifetime of objects<br />Singleton, per-thread, per-session<br />XML configuration<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×