SlideShare a Scribd company logo
IoC and
Dependency injection itself
Discussion
• Why do we need software development
patterns?
• What problems does it solves?
Inversion of control (IoC)
Implementation techniques:
• Factory
• Service Locator
• Dependency Injection (DI)
Factory, Abstract factory
• Used to create objects.
• Created object can be of different types.
Factory: example
public interface IDrink
{
string GetDrinkName();
}
public class Beer : IDrink
{
public string GetDrinkName()
{
return "Hoegaarden";
}
}
public class Whiskey : IDrink
{
public string GetDrinkName()
{
return "Jack Daniel's";
}
}
public static class DrinksFactory
{
public static Whiskey GetWhiskey()
{
return new Whiskey();
}
public static Beer GetBeer()
{
return new Beer();
}
}
class Program
{
static void Main(string[] args)
{
var drink = DrinksFactory.GetBeer();
var name = drink.GetDrinkName();
Console.WriteLine(name);
}
}
Service Locator (SL)
Service – can be considered as an object that
can do something for you as a service.
• Service locator returns objects that already
exist.
Service Locator: example
public class DrinksLocator
{
private IDictionary<object, object> _services;
public DrinksLocator()
{
_services = new Dictionary<object, object>();
}
public void RegisterDrink<TKey,TValue>() where TValue : new()
{
_services.Add(typeof(TKey), new TValue());
}
public T ResolveDrink<T>()
{
return (T) _services[typeof (T)];
}
}
class Program
{
static DrinksLocator _drinksLocator = new DrinksLocator();
static void Configuration()
{
_drinksLocator.RegisterDrink<IWhiskey, Whiskey>();
_drinksLocator.RegisterDrink<IBeer, Beer>();
}
static void Main(string[] args)
{
Configuration();
var drink = _drinksLocator.ResolveDrink<IWhiskey>();
var name = drink.GetDrinkName();
Console.WriteLine(name);
}
}
public interface IDrink
{
string GetDrinkName();
}
internal interface IBeer : IDrink
{
}
internal interface IWhiskey : IDrink
{
}
public class Beer : IBeer
{
public string GetDrinkName()
{
return "Hoegaarden";
}
}
public class Whiskey : IWhiskey
{
public string GetDrinkName()
{
return "Jack Daniel's";
}
}
Dependency Injection (DI)
• Provides you any object (service) of any type
whenever it’s needed.
• You don’t care about it’s creation and
implementation.
DI vs. SL
• The main difference is how the dependencies
are located, in Service Location client code
request the dependencies , in DI we use
container to create all of objects and it injects
dependency as constructor parameters (or
properties)
DI: demo
https://github.com/ohoncharuk/FridayApp.git
https://codio.com/ohoncharuk/FridayApp
DI: pros
• Code is readable and maintainable
• Injected class is a standalone module
• Configurability. Can be even configured
externally.
• Object lifetime management
• Testability
DI: cons
• It requires you to understand how it works.
• Debugging. You might need a reflector 
DI frameworks in .NET
• Autofac - http://autofac.org/
• Ninject - www.ninject.org
• ASP.NET vNext supports DI out of the box
JavaScript & Dependency Injection
• Angular - https://docs.angularjs.org/guide/di
• Ember -
http://guides.emberjs.com/v1.10.0/understanding-
ember/dependency-injection-and-service-lookup/
• Node.js - https://github.com/vojtajina/node-di
Questions?
• Thank you

More Related Content

PPTX
OpenID Connect for W3C Verifiable Credential Objects
PPTX
Self-issued OpenID Provider_OpenID Foundation Virtual Workshop
PPTX
Building secure applications with keycloak
PPT
Identity 2.0 and User-Centric Identity
PPTX
Functional programming in C#
PDF
The Zen of Inversion of Control
PPTX
Identity management and single sign on - how much flexibility
PPTX
Domain Driven Design in Rails
OpenID Connect for W3C Verifiable Credential Objects
Self-issued OpenID Provider_OpenID Foundation Virtual Workshop
Building secure applications with keycloak
Identity 2.0 and User-Centric Identity
Functional programming in C#
The Zen of Inversion of Control
Identity management and single sign on - how much flexibility
Domain Driven Design in Rails

Viewers also liked (9)

PDF
Ruby on Rails
PPTX
No SQL
PPTX
Frontend Workflow
PPTX
Effective email communications with a customer
PPTX
Сборка Front-end’a
PPTX
SOLID
PPTX
React.js
PPTX
Garbage collector
PPTX
RESTful API and ASP.NET
Ruby on Rails
No SQL
Frontend Workflow
Effective email communications with a customer
Сборка Front-end’a
SOLID
React.js
Garbage collector
RESTful API and ASP.NET
Ad

Similar to IoC and Dependency Injection (20)

PPTX
Dependency Injections in Kotlin
PPTX
Docs as Code: Publishing Processes for API Experiences
PDF
SE2016 Java Alex Theedom "Java EE revisits design patterns"
PDF
Alex Theedom Java ee revisits design patterns
PPTX
Verilog & Vivado Quickstart
PPTX
Dependency injection presentation
PDF
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
PDF
Building Top-Notch Androids SDKs
PPTX
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
PPTX
Interop 2017 - Managing Containers in Production
PPT
Android class provider in mumbai
PDF
Docker Birthday #3 Slides - Overview
PDF
Docker Birthday #3 - Intro to Docker Slides
PPTX
20170321 docker with Visual Studio 2017
PPTX
PittsburgJUG_Cloud-Native Dev Tools: Bringing the cloud back to earth
PDF
iOS Development - Offline Class for Jasakomer
PPT
Pertemuan 3 pm
PDF
[WSO2Con EU 2017] Exploring Ballerina Toolset
PDF
Exploring ballerina toolset (docker, testing, tracing, analytics, and more) ...
Dependency Injections in Kotlin
Docs as Code: Publishing Processes for API Experiences
SE2016 Java Alex Theedom "Java EE revisits design patterns"
Alex Theedom Java ee revisits design patterns
Verilog & Vivado Quickstart
Dependency injection presentation
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Building Top-Notch Androids SDKs
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Interop 2017 - Managing Containers in Production
Android class provider in mumbai
Docker Birthday #3 Slides - Overview
Docker Birthday #3 - Intro to Docker Slides
20170321 docker with Visual Studio 2017
PittsburgJUG_Cloud-Native Dev Tools: Bringing the cloud back to earth
iOS Development - Offline Class for Jasakomer
Pertemuan 3 pm
[WSO2Con EU 2017] Exploring Ballerina Toolset
Exploring ballerina toolset (docker, testing, tracing, analytics, and more) ...
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
573137875-Attendance-Management-System-original
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PPTX
Construction Project Organization Group 2.pptx
PPTX
web development for engineering and engineering
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
PPT on Performance Review to get promotions
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Embodied AI: Ushering in the Next Era of Intelligent Systems
573137875-Attendance-Management-System-original
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
Construction Project Organization Group 2.pptx
web development for engineering and engineering
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Operating System & Kernel Study Guide-1 - converted.pdf
PPT on Performance Review to get promotions
CH1 Production IntroductoryConcepts.pptx
Internet of Things (IOT) - A guide to understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

IoC and Dependency Injection

  • 2. Discussion • Why do we need software development patterns? • What problems does it solves?
  • 3. Inversion of control (IoC) Implementation techniques: • Factory • Service Locator • Dependency Injection (DI)
  • 4. Factory, Abstract factory • Used to create objects. • Created object can be of different types.
  • 5. Factory: example public interface IDrink { string GetDrinkName(); } public class Beer : IDrink { public string GetDrinkName() { return "Hoegaarden"; } } public class Whiskey : IDrink { public string GetDrinkName() { return "Jack Daniel's"; } } public static class DrinksFactory { public static Whiskey GetWhiskey() { return new Whiskey(); } public static Beer GetBeer() { return new Beer(); } } class Program { static void Main(string[] args) { var drink = DrinksFactory.GetBeer(); var name = drink.GetDrinkName(); Console.WriteLine(name); } }
  • 6. Service Locator (SL) Service – can be considered as an object that can do something for you as a service. • Service locator returns objects that already exist.
  • 7. Service Locator: example public class DrinksLocator { private IDictionary<object, object> _services; public DrinksLocator() { _services = new Dictionary<object, object>(); } public void RegisterDrink<TKey,TValue>() where TValue : new() { _services.Add(typeof(TKey), new TValue()); } public T ResolveDrink<T>() { return (T) _services[typeof (T)]; } } class Program { static DrinksLocator _drinksLocator = new DrinksLocator(); static void Configuration() { _drinksLocator.RegisterDrink<IWhiskey, Whiskey>(); _drinksLocator.RegisterDrink<IBeer, Beer>(); } static void Main(string[] args) { Configuration(); var drink = _drinksLocator.ResolveDrink<IWhiskey>(); var name = drink.GetDrinkName(); Console.WriteLine(name); } } public interface IDrink { string GetDrinkName(); } internal interface IBeer : IDrink { } internal interface IWhiskey : IDrink { } public class Beer : IBeer { public string GetDrinkName() { return "Hoegaarden"; } } public class Whiskey : IWhiskey { public string GetDrinkName() { return "Jack Daniel's"; } }
  • 8. Dependency Injection (DI) • Provides you any object (service) of any type whenever it’s needed. • You don’t care about it’s creation and implementation.
  • 9. DI vs. SL • The main difference is how the dependencies are located, in Service Location client code request the dependencies , in DI we use container to create all of objects and it injects dependency as constructor parameters (or properties)
  • 11. DI: pros • Code is readable and maintainable • Injected class is a standalone module • Configurability. Can be even configured externally. • Object lifetime management • Testability
  • 12. DI: cons • It requires you to understand how it works. • Debugging. You might need a reflector 
  • 13. DI frameworks in .NET • Autofac - http://autofac.org/ • Ninject - www.ninject.org • ASP.NET vNext supports DI out of the box
  • 14. JavaScript & Dependency Injection • Angular - https://docs.angularjs.org/guide/di • Ember - http://guides.emberjs.com/v1.10.0/understanding- ember/dependency-injection-and-service-lookup/ • Node.js - https://github.com/vojtajina/node-di