SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

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

Más 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
 

Más 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
 

Último

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 

Último (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Dependency Injection vs Service Locator - Best Practice