SlideShare a Scribd company logo
1 of 42
Dependency Injection vs. Service Locator
                      - Best Practice
Lars-Erik Kindblad
Senior Consultant
Blog: kindblad.com
Agenda

 Intro
  •   Inversion of Control
  •   Dependency Injection Patterns
  •   Service Locator Patterns
  •   Inversion of Control Containers
 Best Practice
  • What patterns to use
  • When to use the patterns
 Summary




                                                 | Sector, Alliance, Offering
Inversion of Control

 Inversion of Control = IOC
 Make code loosely coupled
 Make unit testing possible
 How? Move creation of dependencies outside the class they are
  being used in
 A better name - Inversion of Dependency Creation




                                                         | Sector, Alliance, Offering
Traditional Code




Inversion of Control




                       | Sector, Alliance, Offering
Traditional Code




Inversion of Control Code




                            | Sector, Alliance, Offering
Traditional Code




                   | Sector, Alliance, Offering
Dependency Injection - Constructor Injection




                                               | Sector, Alliance, Offering
Dependency Injection - Interface Injection




                                             | Sector, Alliance, Offering
Dependency Injection - Setter Injection




                                          | Sector, Alliance, Offering
Static Service Locator




                         | Sector, Alliance, Offering
Injected Service Locator




                           | Sector, Alliance, Offering
Inversion of Control Container

 A framework that can automatically create a given type with all the
  required dependencies
 Popular frameworks
  •   Unity
  •   Castle Windsor
  •   Ninject
  •   StructureMap
  •   etc.




                                                            | Sector, Alliance, Offering
Inversion of Control Container

         Manual approach




      Using an IOC Container




                                 | Sector, Alliance, Offering
| Sector, Alliance, Offering
Which patterns to use
Traditional Code




                   | Sector, Alliance, Offering
Dependency Injection - Constructor Injection




                                               | Sector, Alliance, Offering
Dependency Injection - Interface Injection




                                             | Sector, Alliance, Offering
Dependency Injection - Setter Injection




                                          | Sector, Alliance, Offering
Static Service Locator




                         | Sector, Alliance, Offering
Injected Service Locator




                           | Sector, Alliance, Offering
When to use which patterns


                   | Sector, Alliance, Offering
Always use Dependency Injection – Constructor Injection




                      ...except
                                                 | Sector, Alliance, Offering
FACADE CLASSES

                 | Sector, Alliance, Offering
Facade Classes




                 | Sector, Alliance, Offering
Dependency Injection




                       | Sector, Alliance, Offering
Service Locator




                  | Sector, Alliance, Offering
LOOPS

        | Sector, Alliance, Offering
Loops




        | Sector, Alliance, Offering
Dependency Injection




                       | Sector, Alliance, Offering
Service Locator




                  | Sector, Alliance, Offering
Dependency Injection using Func<>




                                    | Sector, Alliance, Offering
BASE CLASSES

               | Sector, Alliance, Offering
Base Classes




               | Sector, Alliance, Offering
Dependency Injection




                       | Sector, Alliance, Offering
Service Locator




                  | Sector, Alliance, Offering
UNKNOWN TYPES AT COMPILE TIME

                                | Sector, Alliance, Offering
Unknown types at compile time




                                | Sector, Alliance, Offering
Service Locator




                  | Sector, Alliance, Offering
Summary

                      Constructor Injection   Injected Service Locator

Facade classes                                X
(WCF Services,
MVC Controllers)
Loops                 X (Func<>)              X


Base classes                                  X


Unknown types at                              X
compile time

All other scenarios   X

                                                                    | Sector, Alliance, Offering
QUESTIONS?

             | Sector, Alliance, Offering
www.capgemini.com



The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved

More Related Content

Similar to Dependency Injection vs Service Locator - Best Practice

Formal framework for semantic interoperability in Supply Chain networks
Formal framework for semantic interoperability in Supply Chain networksFormal framework for semantic interoperability in Supply Chain networks
Formal framework for semantic interoperability in Supply Chain networksMilan Zdravković
 
Telecoms' Regulatory Accounting Separation and Oman Case Study
Telecoms' Regulatory Accounting Separation and Oman Case StudyTelecoms' Regulatory Accounting Separation and Oman Case Study
Telecoms' Regulatory Accounting Separation and Oman Case Studyssimam
 
Service Actions
Service ActionsService Actions
Service ActionsOutSystems
 
How to Build Composite Applications with PRISM
How to Build Composite Applications with PRISMHow to Build Composite Applications with PRISM
How to Build Composite Applications with PRISMDataLeader.io
 
Monetizing PaaS and Implementation Models
Monetizing PaaS and Implementation ModelsMonetizing PaaS and Implementation Models
Monetizing PaaS and Implementation ModelsHARMAN Services
 
Introduction to Istio Service Mesh
Introduction to Istio Service MeshIntroduction to Istio Service Mesh
Introduction to Istio Service MeshGeorgios Andrianakis
 
Dependency injection
Dependency injectionDependency injection
Dependency injectionhousecor
 
Layer 7: Cloud Security For The Public Sector
Layer 7: Cloud Security For The Public SectorLayer 7: Cloud Security For The Public Sector
Layer 7: Cloud Security For The Public SectorCA API Management
 
INOVEM Intellect CTO Council 20130225
INOVEM Intellect CTO Council 20130225INOVEM Intellect CTO Council 20130225
INOVEM Intellect CTO Council 20130225Kahootz
 
Applying Design Principles to API Initiatives
Applying Design Principles to API InitiativesApplying Design Principles to API Initiatives
Applying Design Principles to API InitiativesApigee | Google Cloud
 
Corporate Presentation Dec 2010
Corporate Presentation   Dec 2010Corporate Presentation   Dec 2010
Corporate Presentation Dec 2010vivekhalbe
 
Smura 2011 Mobile Platforms Lecture 2
Smura 2011 Mobile Platforms Lecture 2Smura 2011 Mobile Platforms Lecture 2
Smura 2011 Mobile Platforms Lecture 2Timo Smura
 
Building a Novell Data Synchronizer Connector: A Developer's Guide
Building a Novell Data Synchronizer Connector: A Developer's GuideBuilding a Novell Data Synchronizer Connector: A Developer's Guide
Building a Novell Data Synchronizer Connector: A Developer's GuideNovell
 

Similar to Dependency Injection vs Service Locator - Best Practice (18)

Formal framework for semantic interoperability in Supply Chain networks
Formal framework for semantic interoperability in Supply Chain networksFormal framework for semantic interoperability in Supply Chain networks
Formal framework for semantic interoperability in Supply Chain networks
 
Telecoms' Regulatory Accounting Separation and Oman Case Study
Telecoms' Regulatory Accounting Separation and Oman Case StudyTelecoms' Regulatory Accounting Separation and Oman Case Study
Telecoms' Regulatory Accounting Separation and Oman Case Study
 
Service Actions
Service ActionsService Actions
Service Actions
 
Outsourcing
OutsourcingOutsourcing
Outsourcing
 
How to Build Composite Applications with PRISM
How to Build Composite Applications with PRISMHow to Build Composite Applications with PRISM
How to Build Composite Applications with PRISM
 
Monetize PaaS Windows Azure and Implementation Models
Monetize PaaS Windows Azure and Implementation ModelsMonetize PaaS Windows Azure and Implementation Models
Monetize PaaS Windows Azure and Implementation Models
 
Monetizing PaaS and Implementation Models
Monetizing PaaS and Implementation ModelsMonetizing PaaS and Implementation Models
Monetizing PaaS and Implementation Models
 
The Private Cloud, Principles, Patterns and Concepts
The Private Cloud, Principles, Patterns and ConceptsThe Private Cloud, Principles, Patterns and Concepts
The Private Cloud, Principles, Patterns and Concepts
 
Introduction to Istio Service Mesh
Introduction to Istio Service MeshIntroduction to Istio Service Mesh
Introduction to Istio Service Mesh
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Layer 7: Cloud Security For The Public Sector
Layer 7: Cloud Security For The Public SectorLayer 7: Cloud Security For The Public Sector
Layer 7: Cloud Security For The Public Sector
 
Biz Model for AtracTable
Biz Model for AtracTableBiz Model for AtracTable
Biz Model for AtracTable
 
Forecast odcau2 100_posttech
Forecast odcau2 100_posttechForecast odcau2 100_posttech
Forecast odcau2 100_posttech
 
INOVEM Intellect CTO Council 20130225
INOVEM Intellect CTO Council 20130225INOVEM Intellect CTO Council 20130225
INOVEM Intellect CTO Council 20130225
 
Applying Design Principles to API Initiatives
Applying Design Principles to API InitiativesApplying Design Principles to API Initiatives
Applying Design Principles to API Initiatives
 
Corporate Presentation Dec 2010
Corporate Presentation   Dec 2010Corporate Presentation   Dec 2010
Corporate Presentation Dec 2010
 
Smura 2011 Mobile Platforms Lecture 2
Smura 2011 Mobile Platforms Lecture 2Smura 2011 Mobile Platforms Lecture 2
Smura 2011 Mobile Platforms Lecture 2
 
Building a Novell Data Synchronizer Connector: A Developer's Guide
Building a Novell Data Synchronizer Connector: A Developer's GuideBuilding a Novell Data Synchronizer Connector: A Developer's Guide
Building a Novell Data Synchronizer Connector: A Developer's Guide
 

More from Lars-Erik Kindblad

Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014Lars-Erik Kindblad
 
How to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsHow to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsLars-Erik Kindblad
 
Message Oriented Architecture using NServiceBus
Message Oriented Architecture using NServiceBusMessage Oriented Architecture using NServiceBus
Message Oriented Architecture using NServiceBusLars-Erik Kindblad
 
Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Lars-Erik Kindblad
 
Publish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorPublish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorLars-Erik Kindblad
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility PrincipleLars-Erik Kindblad
 
Avoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsAvoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsLars-Erik Kindblad
 

More from Lars-Erik Kindblad (8)

Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014
 
Application Architecture
Application ArchitectureApplication Architecture
Application Architecture
 
How to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsHow to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systems
 
Message Oriented Architecture using NServiceBus
Message Oriented Architecture using NServiceBusMessage Oriented Architecture using NServiceBus
Message Oriented Architecture using NServiceBus
 
Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)
 
Publish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorPublish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event Aggregator
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
 
Avoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsAvoid code duplication! Principles & Patterns
Avoid code duplication! Principles & Patterns
 

Recently uploaded

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
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
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Recently uploaded (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
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
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

Dependency Injection vs Service Locator - Best Practice