SlideShare a Scribd company logo
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

More Related Content

Similar to Dependency Injection

Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
Shahriar 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 Ninject
Oleksandr Valetskyy
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
chhabraravish23
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
Dennis Loktionov
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
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
peychevi
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
meghantaylor
 
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
Theo Jungeblut
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
ssusere19c741
 
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
Enea Gabriel
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast track
Binu Bhasuran
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
devaraj ns
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Giovanni Scerra ☃
 
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
Steven 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 Design
James 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 guidelines
Qamar 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 Influences
Gustavo Andres Brey
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
Knoldus Inc.
 

Similar to 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
 

Recently uploaded

Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
Figma AI Design Generator_ In-Depth Review.pdf
Figma AI Design Generator_ In-Depth Review.pdfFigma AI Design Generator_ In-Depth Review.pdf
Figma AI Design Generator_ In-Depth Review.pdf
Management Institute of Skills Development
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Networks
 
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
digitalxplive
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Zilliz
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
shyamraj55
 
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
Kief Morris
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
aakash malhotra
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
Priyanka Aash
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
Adam Dunkels
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
Vulnerability Management: A Comprehensive Overview
Vulnerability Management: A Comprehensive OverviewVulnerability Management: A Comprehensive Overview
Vulnerability Management: A Comprehensive Overview
Steven Carlson
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
Ivanti
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc
 
CiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.pptCiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.ppt
moinahousna
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
CEPTES Software Inc
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
bhumivarma35300
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
ldtexsolbl
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
kumarjarun2010
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
Neo4j
 

Recently uploaded (20)

Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
Figma AI Design Generator_ In-Depth Review.pdf
Figma AI Design Generator_ In-Depth Review.pdfFigma AI Design Generator_ In-Depth Review.pdf
Figma AI Design Generator_ In-Depth Review.pdf
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
 
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
 
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
[Talk] Moving Beyond Spaghetti Infrastructure [AOTB] 2024-07-04.pdf
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
Vulnerability Management: A Comprehensive Overview
Vulnerability Management: A Comprehensive OverviewVulnerability Management: A Comprehensive Overview
Vulnerability Management: A Comprehensive Overview
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
 
CiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.pptCiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.ppt
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
 

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

Editor's Notes

  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.