• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Dev Cast Dependency Injection
 

Dev Cast Dependency Injection

on

  • 1,718 views

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

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

Statistics

Views

Total Views
1,718
Views on SlideShare
1,698
Embed Views
20

Actions

Likes
4
Downloads
35
Comments
0

3 Embeds 20

http://dylandoesdigits.blogspot.com 13
http://www.slideshare.net 6
http://localhost 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Dev Cast Dependency Injection Dev Cast Dependency Injection Presentation Transcript

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