Dependency Injection


Chester Hartin
Code review 10/21/11
Introduction
   What is dependency injection?
     Constructor  injection
     Setter injection

     Interface Injection

     DI Caution
What is Dependency Injection
   A type of loC (Inversion of Control)
    where we move the creation and
    binding of a dependency outside of
    the class that depends on it
Packing a lunch
Lunch Will Be Provided
Where Do Dependencies Come From?



  MyClass            IDependency




                     Dependency
Where Do Dependencies Come From?


            Injector




MyClass                IDependency




                       Dependency
Constructor Injection
   Most Common
   Simple
     Passdependency into dependent class
     via constructor
Example
public class Injector
  {
    ICreditCard creditCard = new MasterCard();
    Shopper shopper = new Shopper(creditCard);
  }



public class Shopper
  {
    private readonly ICreditCard creditCard;

     public Shopper(ICreditCard creditCard)
     {
       this.creditCard = creditCard;
     }
 }
Setter Injection
   Create a setter on the dependent
    class
   Use the setter to set the
    dependency
Example
public class Injector
  {
    ICreditCard creditCard = new MasterCard();
    Shopper shopper = new Shopper();
    shopper.CreditCard = creditCard;
  }


public class Shopper
  {
    public ICreditCard CreditCard { get; set; }
  }
Interface Injection
   Dependent class implements an
    interface
   Injector uses the interface to set
    the dependency
Example
public class Injector
  {
    ICreditCard creditCard = new MasterCard();
    Shopper shopper = new Shopper();
    ((IDependOnCreditCard)shopper).Inject(creditCard);
  }


 public class Shopper: IDependOnCreditCard
 {
   private ICreditCard creditCard;

     public void Inject(ICreditCard creditCard)
     {
     this.creditCard = creditCard;
     }
 }


 public interface IDependOnCreditCard
 {
   void Inject(ICreditCard creditCard);
 }
DI Caution
   Leaks the internal implementation details
    of a class
       Violates encapsulation
       Injecting “guts” into the class
   Prevents deferred creation
       Dependencies created before needed
   Numbs you from the pain
       Easier to unit test classes that should be
        broken up
       Watch out for too many dependencies
DI Open-Source Libraries
   Unity
   Castle Windsor
   Structure Maps
   Ninject

Dependency injection

  • 1.
  • 2.
    Introduction  What is dependency injection?  Constructor injection  Setter injection  Interface Injection  DI Caution
  • 3.
    What is DependencyInjection  A type of loC (Inversion of Control) where we move the creation and binding of a dependency outside of the class that depends on it
  • 4.
  • 5.
    Lunch Will BeProvided
  • 6.
    Where Do DependenciesCome From? MyClass IDependency Dependency
  • 7.
    Where Do DependenciesCome From? Injector MyClass IDependency Dependency
  • 8.
    Constructor Injection  Most Common  Simple  Passdependency into dependent class via constructor
  • 9.
    Example public class Injector { ICreditCard creditCard = new MasterCard(); Shopper shopper = new Shopper(creditCard); } public class Shopper { private readonly ICreditCard creditCard; public Shopper(ICreditCard creditCard) { this.creditCard = creditCard; } }
  • 10.
    Setter Injection  Create a setter on the dependent class  Use the setter to set the dependency
  • 11.
    Example public class Injector { ICreditCard creditCard = new MasterCard(); Shopper shopper = new Shopper(); shopper.CreditCard = creditCard; } public class Shopper { public ICreditCard CreditCard { get; set; } }
  • 12.
    Interface Injection  Dependent class implements an interface  Injector uses the interface to set the dependency
  • 13.
    Example public class Injector { ICreditCard creditCard = new MasterCard(); Shopper shopper = new Shopper(); ((IDependOnCreditCard)shopper).Inject(creditCard); } public class Shopper: IDependOnCreditCard { private ICreditCard creditCard; public void Inject(ICreditCard creditCard) { this.creditCard = creditCard; } } public interface IDependOnCreditCard { void Inject(ICreditCard creditCard); }
  • 14.
    DI Caution  Leaks the internal implementation details of a class  Violates encapsulation  Injecting “guts” into the class  Prevents deferred creation  Dependencies created before needed  Numbs you from the pain  Easier to unit test classes that should be broken up  Watch out for too many dependencies
  • 15.
    DI Open-Source Libraries  Unity  Castle Windsor  Structure Maps  Ninject