SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus de 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
 

Dernier

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 

Dernier (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

Dependency Injection vs Service Locator - Best Practice