SlideShare una empresa de Scribd logo
1 de 42
DEPENDENCY INJECTION
PART 1
Alastair Smith (after Mark Seemann)
Agenda
 Overview
 Concepts
 The Scope of Dependency Injection
 Patterns and Anti-Patterns
What is Dependency Injection?
What is Dependency Injection Not?
What Does Dependency Injection BuyYou?
Overview
What is Dependency
Injection?
 A technique
 A collection of patterns for loosely-coupled
code
 Favour composition over inheritance
 Program to the interface not the implementation
 A form of Inversion of Control
What is Dependency Injection
Not?
 Dependency Inversion (the D in SOLID)
 You must first invert your dependencies to
properly apply dependency injection
Data Access
Layer
Business
Logic Layer
Presentation
Layer
What Does DI Buy You?
 Testable and maintainable code
 Encourages SOLID code
 Late binding
 Swapping services out for other implementations
 Easy extensibility
 Code can be extended in ways not originally
envisaged
The Purpose of DI
What to Inject (andWhat Not to Inject)
Concepts
Tight Coupling
Wall
How do we decouple?
Wall
How do we get new
functionality?
Wall
UPS
How do we do multiple
things?
Wall
UPS
How do we use an
incompatible device?
Wall
UPS
Design Patterns will only
get you so far
 How you assemble your bricks matters
What to Inject
 VOLATILE DEPENDENCIES
libraries that:
 introduce the need to configure an external
environment
 are still in development
 aren’t installed on all machines in the
development organisation
 E.g.: EntityBroker, Phoenix, SpreadsheetGear
 Inject across SEAMS
What Not to Inject
 STABLE DEPENDENCIES
libraries or frameworks that:
 won’t introduce breaking changes with new
versions
 you never expect to have to replace
 E.g.: the BCL
Object Composition
Lifetime Management
Interception
The Scope of Dependency Injection
Object Composition
 Building object graphs into functional units
 Creating objects
 Creating relationships between objects
 No more new Foo()
 Single Responsibility Principle
 Regain control through a DI container
 Provides late-binding, extensibility and
parallel development benefits
Lifetime Management
 Must invert control of an object’s lifetime
management as well as the dependency itself
 Different lifetime options
 Singleton
 Per-request
 Per-thread
 (and others)
A Word on IDisposable
 Complicated by use of Dependency Injection
 Do not mark your abstractions as
IDisposable
 Let the DI Container handle disposable
implementations
 Hide Ephemeral Disposables behind a SEAM
 Use an injectedAbstract Factory to create them
 Keep their using blocks short
Interception
 Applying functionality on the fly
 Used to add Cross-Cutting Concerns:
 Logging
 Auditing
 Performance
 Access control
 Security
 ...
Aspect-Oriented Programming
 Cross-Cutting Concerns applied via attributes
decorating a method
 [PrincipalPermission]
 [HandleError]
 Requires either:
 Post-compilation step (e.g., PostSharp), or
 A custom host (e.g.,WCF, ASP.NET MVC, NUnit)
AOP Disadvantages
 Attributes are compiled with the code they
adorn
 Can’t easily change behaviour
 Limited options for applying attributes
 Attributes must have a simple constructor
 Makes lifetime management trickier
Dynamic Interception
 Can be achieved with liberal use of
Decorators
 Violates DRY
 Lots of boilerplate code
 Container-level feature
 CastleWindsor, Spring.NET, Unity and Ninject all
support it
 Neat way to achieve DRY and SOLID code
Constructor Injection
Property Injection
Method Injection
Ambient Context
Patterns
Terminology
 COMPOSITION ROOT
 Where the application is assembled
 DEPENDENCY
 AVolatile Dependency from Part 1
 DEFAULT value
 E.g. a no-op, such as a Null Object
 Not your default implementation!
Constructor Injection
 This should be your default choice for DI
 Guarantees that a necessary DEPENDENCY is
always available to the class
Advantages Disadvantages
Injection guaranteed Some frameworks make it
difficult
Easy to implement
Property Injection
 Use when a DEPENDENCY is optional
 There should be a good LOCAL DEFAULT
Advantages Disadvantages
Easy to understand Limited applicability
Not entirely simple to
implement robustly
Method Injection
 Use when a DEPENDENCY varies per call of a
method
 Usually the DEPENDENCY represents some
context for the method call
 Limited applicability
Advantages Disadvantages
Allows the caller to provide
operation-specific context
Limited applicability
Ambient Context
 Use to inject static DEPENDENCIES
 Only use when querying the dependency
 A proper LOCAL DEFAULT exists
 It must be guaranteed available
Advantages Disadvantages
Doesn’t polluteAPIs Implicit
Is always available Hard to implement correctly
May not work well in some
runtimes
Control Freak
Bastard Injection
Service Locator
ConstrainedConstruction
Anti-Patterns
Control Freak
 DEPENDENCIES are controlled directly
 E.g., creating an instance of Foo for a field of type
IFoo.
 Usually occurs within a Factory
 Root these out of your codebase!
 Refactor to Constructor Injection
Bastard Injection
 Foreign DEFAULTS are used as default values
for DEPENDENCIES
 E.g., AccountController in ASP.NET MVC
 Refactor towards Constructor Injection
 Refactor towards Property Injection
 But only if there’s a LOCAL DEFAULT
Service Locator
 Calling into your IoC container outside of the
COMPOSITION ROOT
 Can tightly-couple your application to your
IoC container!
 Acts as a replacement for new Foo()
 Refactor towards Constructor Injection
Constrained Construction
 Constructors are assumed to have a particular
signature
 E.g.,WebForms requires Pages to have a default
constructor
 No easy refactoring to DI
 Some possibilities via Abstract Factory
Any questions?
End of Part One
DEPENDENCY INJECTION
PART 2
Alastair Smith (after Mark Seemann)
ASP.NET MVC
WPF
WCF
ASP.NET
Manually Composing Applications
ASP.NET MVC
 COMPOSITION ROOT: global.asax +
IControllerFactory
 Can also useWebActivator package from NuGet
 Ignore IDependencyResolver!
 Intended to be a Service Locator
 Removes ability to manage object lifetime
 IControllerFactory is the appropriate
extensibility point
 Use Constructor Injection on your Controllers
WPF with MVVM
 COMPOSITION ROOT:
Application.OnStartup()
 Use Constructor Injection on your
MainWindowViewModel
 Create an IWindow interface and inject this
 Implemented by a MainWindowAdapter for each
Window
 Create a MainWindowViewModelFactory
 Needed because there is a circular dependency
between theView and theViewModel
WCF
 COMPOSITION ROOT: triplet of
 ServiceHostFactory
 ServiceHost
 IInstanceProvider
ASP.NET
 Suffers from Constrained Construction
 COMPOSITION ROOT: each Page class
 Single Responsibility Principle
 Model-View-Presenter (MVP)
 Keep Presenters fully independent of
System.Web
Any questions?
End of Part Two

Más contenido relacionado

Similar a Dependency Injection

Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion PrincipleShahriar Hyder
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversionchhabraravish23
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoCDennis Loktionov
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 
Best practices for creating modular Web applications
Best practices for creating modular Web applicationsBest practices for creating modular Web applications
Best practices for creating modular Web applicationspeychevi
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckTheo Jungeblut
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETssusere19c741
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstEnea Gabriel
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast trackBinu Bhasuran
 
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Steven Smith
 
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group Theo Jungeblut
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignJames Phillips
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Knoldus Inc.
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Agile Open 2009 Tdd And Architecture Influences
Agile Open 2009   Tdd And Architecture InfluencesAgile Open 2009   Tdd And Architecture Influences
Agile Open 2009 Tdd And Architecture InfluencesGustavo Andres Brey
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionKnoldus Inc.
 

Similar a Dependency Injection (20)

Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Best practices for creating modular Web applications
Best practices for creating modular Web applicationsBest practices for creating modular Web applications
Best practices for creating modular Web applications
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast track
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013
 
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Agile Open 2009 Tdd And Architecture Influences
Agile Open 2009   Tdd And Architecture InfluencesAgile Open 2009   Tdd And Architecture Influences
Agile Open 2009 Tdd And Architecture Influences
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 

Último

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Dependency Injection

  • 1. DEPENDENCY INJECTION PART 1 Alastair Smith (after Mark Seemann)
  • 2. Agenda  Overview  Concepts  The Scope of Dependency Injection  Patterns and Anti-Patterns
  • 3. What is Dependency Injection? What is Dependency Injection Not? What Does Dependency Injection BuyYou? Overview
  • 4. What is Dependency Injection?  A technique  A collection of patterns for loosely-coupled code  Favour composition over inheritance  Program to the interface not the implementation  A form of Inversion of Control
  • 5. What is Dependency Injection Not?  Dependency Inversion (the D in SOLID)  You must first invert your dependencies to properly apply dependency injection Data Access Layer Business Logic Layer Presentation Layer
  • 6. What Does DI Buy You?  Testable and maintainable code  Encourages SOLID code  Late binding  Swapping services out for other implementations  Easy extensibility  Code can be extended in ways not originally envisaged
  • 7. The Purpose of DI What to Inject (andWhat Not to Inject) Concepts
  • 9. How do we decouple? Wall
  • 10. How do we get new functionality? Wall UPS
  • 11. How do we do multiple things? Wall UPS
  • 12. How do we use an incompatible device? Wall UPS
  • 13. Design Patterns will only get you so far  How you assemble your bricks matters
  • 14. What to Inject  VOLATILE DEPENDENCIES libraries that:  introduce the need to configure an external environment  are still in development  aren’t installed on all machines in the development organisation  E.g.: EntityBroker, Phoenix, SpreadsheetGear  Inject across SEAMS
  • 15. What Not to Inject  STABLE DEPENDENCIES libraries or frameworks that:  won’t introduce breaking changes with new versions  you never expect to have to replace  E.g.: the BCL
  • 17. Object Composition  Building object graphs into functional units  Creating objects  Creating relationships between objects  No more new Foo()  Single Responsibility Principle  Regain control through a DI container  Provides late-binding, extensibility and parallel development benefits
  • 18. Lifetime Management  Must invert control of an object’s lifetime management as well as the dependency itself  Different lifetime options  Singleton  Per-request  Per-thread  (and others)
  • 19. A Word on IDisposable  Complicated by use of Dependency Injection  Do not mark your abstractions as IDisposable  Let the DI Container handle disposable implementations  Hide Ephemeral Disposables behind a SEAM  Use an injectedAbstract Factory to create them  Keep their using blocks short
  • 20. Interception  Applying functionality on the fly  Used to add Cross-Cutting Concerns:  Logging  Auditing  Performance  Access control  Security  ...
  • 21. Aspect-Oriented Programming  Cross-Cutting Concerns applied via attributes decorating a method  [PrincipalPermission]  [HandleError]  Requires either:  Post-compilation step (e.g., PostSharp), or  A custom host (e.g.,WCF, ASP.NET MVC, NUnit)
  • 22. AOP Disadvantages  Attributes are compiled with the code they adorn  Can’t easily change behaviour  Limited options for applying attributes  Attributes must have a simple constructor  Makes lifetime management trickier
  • 23. Dynamic Interception  Can be achieved with liberal use of Decorators  Violates DRY  Lots of boilerplate code  Container-level feature  CastleWindsor, Spring.NET, Unity and Ninject all support it  Neat way to achieve DRY and SOLID code
  • 24. Constructor Injection Property Injection Method Injection Ambient Context Patterns
  • 25. Terminology  COMPOSITION ROOT  Where the application is assembled  DEPENDENCY  AVolatile Dependency from Part 1  DEFAULT value  E.g. a no-op, such as a Null Object  Not your default implementation!
  • 26. Constructor Injection  This should be your default choice for DI  Guarantees that a necessary DEPENDENCY is always available to the class Advantages Disadvantages Injection guaranteed Some frameworks make it difficult Easy to implement
  • 27. Property Injection  Use when a DEPENDENCY is optional  There should be a good LOCAL DEFAULT Advantages Disadvantages Easy to understand Limited applicability Not entirely simple to implement robustly
  • 28. Method Injection  Use when a DEPENDENCY varies per call of a method  Usually the DEPENDENCY represents some context for the method call  Limited applicability Advantages Disadvantages Allows the caller to provide operation-specific context Limited applicability
  • 29. Ambient Context  Use to inject static DEPENDENCIES  Only use when querying the dependency  A proper LOCAL DEFAULT exists  It must be guaranteed available Advantages Disadvantages Doesn’t polluteAPIs Implicit Is always available Hard to implement correctly May not work well in some runtimes
  • 30. Control Freak Bastard Injection Service Locator ConstrainedConstruction Anti-Patterns
  • 31. Control Freak  DEPENDENCIES are controlled directly  E.g., creating an instance of Foo for a field of type IFoo.  Usually occurs within a Factory  Root these out of your codebase!  Refactor to Constructor Injection
  • 32. Bastard Injection  Foreign DEFAULTS are used as default values for DEPENDENCIES  E.g., AccountController in ASP.NET MVC  Refactor towards Constructor Injection  Refactor towards Property Injection  But only if there’s a LOCAL DEFAULT
  • 33. Service Locator  Calling into your IoC container outside of the COMPOSITION ROOT  Can tightly-couple your application to your IoC container!  Acts as a replacement for new Foo()  Refactor towards Constructor Injection
  • 34. Constrained Construction  Constructors are assumed to have a particular signature  E.g.,WebForms requires Pages to have a default constructor  No easy refactoring to DI  Some possibilities via Abstract Factory
  • 36. DEPENDENCY INJECTION PART 2 Alastair Smith (after Mark Seemann)
  • 38. ASP.NET MVC  COMPOSITION ROOT: global.asax + IControllerFactory  Can also useWebActivator package from NuGet  Ignore IDependencyResolver!  Intended to be a Service Locator  Removes ability to manage object lifetime  IControllerFactory is the appropriate extensibility point  Use Constructor Injection on your Controllers
  • 39. WPF with MVVM  COMPOSITION ROOT: Application.OnStartup()  Use Constructor Injection on your MainWindowViewModel  Create an IWindow interface and inject this  Implemented by a MainWindowAdapter for each Window  Create a MainWindowViewModelFactory  Needed because there is a circular dependency between theView and theViewModel
  • 40. WCF  COMPOSITION ROOT: triplet of  ServiceHostFactory  ServiceHost  IInstanceProvider
  • 41. ASP.NET  Suffers from Constrained Construction  COMPOSITION ROOT: each Page class  Single Responsibility Principle  Model-View-Presenter (MVP)  Keep Presenters fully independent of System.Web

Notas del editor

  1. Ultimately, it’s just a fancy name for passing stuff into classes. BUT its ramifications are significant.
  2. Dependency Inversion PrincipleHigh-level modules should not depend on low-level modules. Both should depend on abstractionsAbstractions should not depend on details. Details should depend upon abstractions.Your application’s abstractions should be in the BLL, therefore it is wrong for the BLL to depend on the DAL.
  3. Encourages SOLID code = plays very nicely with SOLID code. SRP makes composing dependencies easier, amongst other things.
  4. DI is a means to achieving loosely-coupled codeThe TV’spower is wired directly into the mains : it’s tightly coupled
  5. We can now plug in our laptop insteadUnplugging doesn’t cause either the TV or the wall to explodeWe’ve added an interface!
  6. We get new functionalityWe’ve added a Decorator!
  7. We can now plug two things into the one socketWe’ve added a Composite!
  8. We can now plug in a foreign device, or something small like a camera or phoneWe’ve added an Adapter!
  9. Design patterns are building blocks, bricks. How you assemble your bricks mattersAnd this is the great thing about DI: you can add new functionality to new classes (obeying SRP and OCP via decorators), and include it by simply altering the way the application is put together.
  10. First one is interesting: EntityBroker is an obvious example, but less obvious examples include the .NET FileSystem classes. Not saying don’t use these, don’t rely on them: DO. Just inject them as dependencies to your class.A Seam is anywhere an interface is used over a concrete type.like the seams of a garment: where an application comes together. E.g., data access layer and business logic layer have a seam.
  11. Stable Dependencies: essentially any framework (e.g., ASP.NET MVC, Unit Testing), the BCL, IoC containersSome exceptions to the “Don’t inject the BCL” rule: if a dependency is not easily mockable, such as HttpContext which is a sealed class, wrap it in an interface and Adapter, and inject that.
  12. Three dimensions of DI. Object Composition: building a graph of dependenciesLifetime Management: creating new instances of dependencies, scoping dependencies appropriately, correctly disposing of dependencies when they are no longer neededInterception:Application of the Decorator patternAspect-Oriented Programming
  13. Single Repsonsibility Principle states that a class should only have one reason to change. If a class is manually managing its dependencies by creating them, that is a reason to change. To adhere to the SRP, the class must surrender control of its dependencies; you regain control of those dependencies through the container.
  14. In the previous slide, we removed the ability to create dependencies. As a result, we lose the ability to dispose of them as well, and we must fully delegate management of the object’s lifetime up the stack.Choosing the right one is important for application performance and avoiding resource leaksDo not confuse the Singleton lifetime with the Singleton pattern!
  15. Abstractions that are disposable are leaky abstractions: i.e., it betrays detail about the expected implementation behaviour. Even if you have a particular implementation in mind, avoid the urge to mark the interface as IDisposable. It’s like saying your IMainsPlug is an IWatchable: you can watch a PS3, but it’ll be waaay less interesting than watching a TV. .NET Framework guidelines insist that if a class holds an IDisposable member, it should itself implement IDisposable so that it can dispose of the disposable members. If you have a dependency with Singleton scope injected into a class that, and the class follows this recommendation, you will start seeing ObjectDisposedExceptions all over your code.Ephemeral Disposables are common in WCF: channels are created and disposed for each service operation. This is a complicated topic and I can’t do it proper justice here. See Chapter 8 for the full story.
  16. Cross-Cutting Concerns are areas of the application that need to be applied to all levels of the application, and that can be considered as common functionality required by many applications. I.e., NOT application-specificNote that they are separate from cross-cutting entities, which should be avoided. Cross-cutting entities are business objects valid in any layer of your application; however, “when entities are allowed to travel between layers, the layers basically collapse. UI concerns and data access concerns will inevitably be mixed up. You may think you have layers, but you don’t.” (http://blog.ploeh.dk/2012/02/09/IsLayeringWorthTheMapping.aspx)E.g., Units
  17. Both [PrincipalPermission] and [HandleError] are examples from the .NET framework: the BCL and ASP.NET MVC respectively.
  18. Must re-compile code in order to change behaviour based on the attributes defined.Attributes can only be applied in certain places: type definitions, method definitions, etc.Attributes can only have simple constructors because of how they are used. Can’t pass in instance variables, local variables, etc.
  19. Decorator isa great pattern for intercepting and modifying behaviour on the fly. However, it results in a lot of repetitive boilerplate code. We can instead take advantage of an IoC container-level feature, where supported, to achieve full dynamic interception.Container dynamically emits new types implementing the required aspect. You must still write the code to implement the aspect, but then you can just tell the container about the aspect and when to apply it. Usually a case of implementing an interface defined by the IoC container for the interceptor. Sometimes you might need to do the same for the interception itself as well. Ninject.Extensions.Interception provides IInterceptor and IInvocation interfaces. IInterceptor defines a single method, Intercept(IInvocation), but it’s recommended to use either ActionInterceptor or SimpleInterceptor if you can.
  20. Volatile dependencies = dependencies thatintroduce the need to configure an external environmentare still in developmentaren’t installed on all machines in the development organisationComposition Root is not always the application bootstrapper; in web applications, for example, it might be the beginning of a request. Depends on the framework you’re using (if any).
  21. Example: Blog.BusinessLogic.Implementation.PostService
  22. .NET Example: System.ComponentModel.IComponent.Site takes an ISite, which is mostly used by Visual Studio to support extra designer functionality.One of many ways of implementing the Open-Closed Principle.Example: Blog.BusinessLogic.Implementation.PostService.TagService (contrived example)
  23. No example in my Blog sample, just couldn’t come up with one. Example in the book is one of a currency convertor.NET examples: System.ComponentModel.TypeConverter.ConvertTo() takes an ITypeDescriptorContext; IModelBinder.BindModel() takes a ControllerContext and a ModelBindingContext.Whilst Constructor Injection is useful for applications built on frameworks, Method Injection can be particularly useful for building frameworks themselves. Counter-example: IRatingAlgorithm.CalculateRating: neither the Rating nor the integer “score” parameters are dependencies, so this is not an example of Method Injection.
  24. Should only be used in the rarest of cases: please avoid static dependencies, not least because it makes unit testing really hard!Querying the dependency: don’t use for e.g., Logging, where all methods of the dependency return void. This situation is better modelled using Interception.Examples from the BCL: Thread.CurrentPrincipal, Thread.Current[UI]CultureExample: LoggingService
  25. No easy sample to show for this one, unfortunately.
  26. Ayende talk, November 2010. A question he gets asked a lot is how he manages to do so much stuff. Aim: get to “done” as quickly and easily as possible.Nested Composite Design  • Very modular  • Layered from very low-level to high-level  • Manages complexity very well  • High orthogonality  • Requires some kind of container  • Self-composing  • Loose components  • Lends itself well to extensibilityWhy?  • Attack the problem in small increments  • whole is greater than sum of its parts  • naturally creates isolated pieces of code  • users can touch just one partDependency Injection enables this.
  27. IDependencyResolver is intended to be used as a Service Locator (Antipattern!), and is used by ASP.NET MVC as such. It also lacks a Release() method, meaning that objects cannot be cleaned up after use: we’ve lost the ability to manage object lifetime, and so it will cause resource leaks when used in conjunction with some DI containers like Castle Windsor. However, some containers (e.g. Ninject) implement IDependencyResolver to hook the container into MVC. This is ok as the use is tightly restricted and regulated; no dependencies other than the container, which has no lifetime difficulties of its own, are resolved this way.
  28. Poor design choice in WPF: DataContext is a property, which implies the dependency is optional. However, the DataContext is not optional when developing in MVVM, the pattern prescribed for WPF development by… Microsoft!
  29. Hard work, but not impossible.Because the Page class is now our composition root, its responsibility is to compose the dependencies and we must delegate the actual page logic elsewhere. MVP the key. Keep Presenters in a separate PresentationLogic assembly with no reference to System.Web: that’s the View layer.Alternative is to use a Service Locator, which is a Bad Thing™, particularly here as it clouds the responsibility of the Page class. Unfortunately this is the approach that Viewer currently takes.