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

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Dependency Injection vs Service Locator - Best Practice