SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Kürzlich hochgeladen (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Dependency Injection vs Service Locator - Best Practice