SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6Amir Barylko
 
Fluent Validation Framework - A DSL for validations using fluent interfaces
Fluent Validation Framework - A DSL for validations using fluent interfacesFluent Validation Framework - A DSL for validations using fluent interfaces
Fluent Validation Framework - A DSL for validations using fluent interfacesDaniel Gazineu
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architectureRichard Banks
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6BlackRabbitCoder
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use itnspyre_net
 
CQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility SegregationCQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility SegregationBrian Ritchie
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core 04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core Cellenza
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Bishoy Demian
 
Six safe fonts to use in your presentations
Six safe fonts to use in your presentationsSix safe fonts to use in your presentations
Six safe fonts to use in your presentationsPresentitude
 

Andere mochten auch (14)

What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6
 
Fluent Validation Framework - A DSL for validations using fluent interfaces
Fluent Validation Framework - A DSL for validations using fluent interfacesFluent Validation Framework - A DSL for validations using fluent interfaces
Fluent Validation Framework - A DSL for validations using fluent interfaces
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architecture
 
Fluent validation
Fluent validationFluent validation
Fluent validation
 
Let's Explore C# 6
Let's Explore C# 6Let's Explore C# 6
Let's Explore C# 6
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use it
 
CQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility SegregationCQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility Segregation
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core 04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
Six safe fonts to use in your presentations
Six safe fonts to use in your presentationsSix safe fonts to use in your presentations
Six safe fonts to use in your presentations
 
Slides That Rock
Slides That RockSlides That Rock
Slides That Rock
 

Mehr von Chester Hartin

Mehr von Chester Hartin (8)

Xamarin 101
Xamarin 101Xamarin 101
Xamarin 101
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Elapsed time
Elapsed timeElapsed time
Elapsed time
 
Map kit
Map kitMap kit
Map kit
 
Hash tables
Hash tablesHash tables
Hash tables
 
Parallel extensions
Parallel extensionsParallel extensions
Parallel extensions
 
Reflection
ReflectionReflection
Reflection
 

Dependency injection

  • 2. Introduction  What is dependency injection?  Constructor injection  Setter injection  Interface Injection  DI Caution
  • 3. 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
  • 5. Lunch Will Be Provided
  • 6. Where Do Dependencies Come From? MyClass IDependency Dependency
  • 7. Where Do Dependencies Come 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