SlideShare a Scribd company logo
Dependency Injection 
With Unity Container 
Presenter: Kaveri Saxena, Mindfire Solutions 
Date: 28/04/2014
KKaavveerrii SSaaxxeennaa 
WWoorrkkiinngg WWiitthh MMiinnddffiirree ssiinnccee JJuullyy 11,,22001133 
WWiinnddoowwss DDeesskkttoopp AApppplliiccaattiioonn DDeevveellooppeerr (CC##,, ..NNEETT)) 
SSkkyyppee:: mmffssii__kkaavveerriiss 
EEmmaaiill :: kkaavveerriiss@@mmiinnddffiirreessoolluuttiioonnss..ccoomm 
Presenter: Kaveri Saxena, Mindfire Solutions
Presenter: Kaveri Saxena, Mindfire Solutions 
AGENDA 
●Overview Of Dependency Injection 
●Why Dependency Injection? 
●Constructor Injection 
●Setter Injection 
●Interface Injection 
●Overview of DI Containers 
●Demo Implementing Dependency Injection with Unity Container 
●Q&A
Overview Of Dependency Injection 
Dependency injection is a software design pattern that allows 
the removal of hard-coded dependencies and makes it possible 
to change them,whether at run time or compile time. 
Dependency injection is a set of software design principles and 
Pattern that enable us to develop loosely coupled codes. 
- Mark Seeman 
Tight 
Coupling 
Form 
(Class) Repository 
Var repository = new Repository();
Why To Use DI????? 
●Extensibility 
●Testability 
●Late Binding 
●Parallel Development 
●Maintainability
Constructor Injection 
Pass dependency into dependent class via constructor 
var service = new SQLService(); 
Var form = new PersonForm(service); 
Application.Run(form); 
private IServiceRepository Repository; 
public PersonForm(IServiceRepository repository) 
{ 
Repository = repository; 
}
Setter Injection 
Create a setter on the dependent class and use that to set the 
dependency. 
var form = new PersonForm(); 
form.Repository = new CSVService(); 
Application.Run(form); 
private IServiceRepository Repository { get; set; }
Interface Injection 
Dependent class implements an interface and injector uses 
the interface to set the dependency. 
var service = new SQLService(); 
var form = new PersonForm(); 
((IInjectInterface)form).inject(service); 
Application.Run(form); 
public interface IInjectInterface 
{ 
void inject(IServiceRepository serviceRepo); 
}
public partial class PersonForm :Form,IInjectInterface 
{ 
public void inject(IServiceRepository serviceRepo) 
{ 
Repository = serviceRepo; 
} 
}
Overview Of DI Container 
●A framework for doing dependency Injection 
●Configure dependencies 
●Automatically resolves configured dependencies 
Eg. Unity,Ninject,,CastleWindsor,Autofac,structuremap, 
Spring.Net and many others. 
SqlService 
DI Container 
CSVService 
IService 
Person Form
Unity Container 
Unity comes from microsoft pattern and practice team, 
available as free download. 
For using Unity Container- 
●Download compatible version of unity in your bootstrapper 
Application 
● Register interface with concrete type 
●Call resolve method and unity will automatically find complex 
constructor of dependent class and use concrete type instead 
of interface. 
var container = new UnityContainer(); 
container.RegisterType<IServiceRepository, 
SQLService>(); 
var form = container.Resolve<PersonForm>(); 
Application.Run(form);
Question and 
Answer 
Presenter: Kaveri Saxena, Mindfire Solutions
Thank you 
Presenter: Kaveri Saxena, Mindfire Solutions
www.mindfiresolutions.com 
https://www.facebook.com/MindfireSolutions 
http://www.linkedin.com/company/mindfire-solutions 
http://twitter.com/mindfires
References: 
http://www.pluralsight.com/courses/dependency-injection-on-ramp

More Related Content

PPTX
Dependency injection - the right way
PPTX
Inversion of Control and Dependency Injection
PPTX
Dependency Injection Inversion Of Control And Unity
PDF
Jug Guice Presentation
PPTX
Functional Dependency Injection in C#
PPTX
Introduction to Google Guice
PPT
Silverlight 2 for Developers - TechEd New Zealand 2008
PDF
New Features of JSR 317 (JPA 2.0)
Dependency injection - the right way
Inversion of Control and Dependency Injection
Dependency Injection Inversion Of Control And Unity
Jug Guice Presentation
Functional Dependency Injection in C#
Introduction to Google Guice
Silverlight 2 for Developers - TechEd New Zealand 2008
New Features of JSR 317 (JPA 2.0)

What's hot (20)

PDF
MongoDB Stitch Tutorial
PPTX
Developing ASP.NET Applications Using the Model View Controller Pattern
PPT
PDF
Dependency Injection
PDF
Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"
PDF
Java Svet - Communication Between Android App Components
PPTX
Agile methodologies based on BDD and CI by Nikolai Shevchenko
PPTX
Jasig Cas High Availability - Yale University
PDF
Distributing information on iOS
PPT
PDF
Introduction to Spring MVC
PPTX
Wiesław Kałkus: C# functional programming
PDF
Ngrx meta reducers
PDF
Proxy design pattern (Class Ambassador)
PPSX
Proxy design pattern
ODP
CDI @javaonehyderabad
PPTX
Design pattern proxy介紹 20130805
PPTX
Functional programming in C#
PPTX
Introduction to OO, Java and Eclipse/WebSphere
PPTX
Proxy Pattern
MongoDB Stitch Tutorial
Developing ASP.NET Applications Using the Model View Controller Pattern
Dependency Injection
Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"
Java Svet - Communication Between Android App Components
Agile methodologies based on BDD and CI by Nikolai Shevchenko
Jasig Cas High Availability - Yale University
Distributing information on iOS
Introduction to Spring MVC
Wiesław Kałkus: C# functional programming
Ngrx meta reducers
Proxy design pattern (Class Ambassador)
Proxy design pattern
CDI @javaonehyderabad
Design pattern proxy介紹 20130805
Functional programming in C#
Introduction to OO, Java and Eclipse/WebSphere
Proxy Pattern
Ad

Viewers also liked (10)

PDF
Build Reusable Web Components using HTML5 Web cComponents
PPTX
SOLID Principles Part 3
PPTX
Inversion of control
PDF
Introduction to SOLID Principles
PPTX
Open Closed Principle kata
PPTX
IoC and Mapper in C#
PPTX
Dependency Inversion Principle
PPTX
Inversion of Control in MVC
PPT
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
PPTX
SOLID - Principles of Object Oriented Design
Build Reusable Web Components using HTML5 Web cComponents
SOLID Principles Part 3
Inversion of control
Introduction to SOLID Principles
Open Closed Principle kata
IoC and Mapper in C#
Dependency Inversion Principle
Inversion of Control in MVC
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
SOLID - Principles of Object Oriented Design
Ad

Similar to Dependency Injection with Unity Container (20)

PDF
JavaCro'15 - Web UI best practice integration with Java EE 7 - Peter Lehto
PPTX
Dependency Injection in Drupal 8
PDF
Spring Framework-II
PDF
Techlunch - Dependency Injection with Vaadin
PDF
Swiz DAO
PPTX
High Performance Cloud Native APIs Using Apache Geode
PPTX
MVVM Design Pattern NDC2009
PPTX
Dependency Injection або Don’t call me, I’ll call you
DOCX
Critical Analysis of SW Development tool/methodology
PPTX
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
PDF
Angular Dependency Injection
PPTX
.NET Intro & Dependency Injection Workshop
PDF
Introduction to AngularJS
PDF
FedScoop Public Sector Innovation Summit DOD Enterprise DevSecOps Initiative ...
PPT
Angular Seminar-js
DOCX
PPTX
The Quest for Continuous Delivery at Pluralsight
PDF
Iddd ch14
PDF
Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...
PDF
JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...
JavaCro'15 - Web UI best practice integration with Java EE 7 - Peter Lehto
Dependency Injection in Drupal 8
Spring Framework-II
Techlunch - Dependency Injection with Vaadin
Swiz DAO
High Performance Cloud Native APIs Using Apache Geode
MVVM Design Pattern NDC2009
Dependency Injection або Don’t call me, I’ll call you
Critical Analysis of SW Development tool/methodology
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Angular Dependency Injection
.NET Intro & Dependency Injection Workshop
Introduction to AngularJS
FedScoop Public Sector Innovation Summit DOD Enterprise DevSecOps Initiative ...
Angular Seminar-js
The Quest for Continuous Delivery at Pluralsight
Iddd ch14
Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...
JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...

More from Mindfire Solutions (20)

PDF
Physician Search and Review
PDF
diet management app
PDF
Business Technology Solution
PDF
Remote Health Monitoring
PDF
Influencer Marketing Solution
PPT
High Availability of Azure Applications
PPTX
IOT Hands On
PPTX
Glimpse of Loops Vs Set
ODP
Oracle Sql Developer-Getting Started
PPT
Adaptive Layout In iOS 8
PPT
Introduction to Auto-layout : iOS/Mac
PPT
LINQPad - utility Tool
PPT
Get started with watch kit development
PPTX
Swift vs Objective-C
ODP
Material Design in Android
ODP
Introduction to OData
PPT
Ext js Part 2- MVC
PPT
ExtJs Basic Part-1
PPT
Spring Security Introduction
Physician Search and Review
diet management app
Business Technology Solution
Remote Health Monitoring
Influencer Marketing Solution
High Availability of Azure Applications
IOT Hands On
Glimpse of Loops Vs Set
Oracle Sql Developer-Getting Started
Adaptive Layout In iOS 8
Introduction to Auto-layout : iOS/Mac
LINQPad - utility Tool
Get started with watch kit development
Swift vs Objective-C
Material Design in Android
Introduction to OData
Ext js Part 2- MVC
ExtJs Basic Part-1
Spring Security Introduction

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
top salesforce developer skills in 2025.pdf
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Odoo Companies in India – Driving Business Transformation.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
history of c programming in notes for students .pptx
Design an Analysis of Algorithms II-SECS-1021-03
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Computer Software and OS of computer science of grade 11.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Operating system designcfffgfgggggggvggggggggg
Which alternative to Crystal Reports is best for small or large businesses.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......

Dependency Injection with Unity Container

  • 1. Dependency Injection With Unity Container Presenter: Kaveri Saxena, Mindfire Solutions Date: 28/04/2014
  • 2. KKaavveerrii SSaaxxeennaa WWoorrkkiinngg WWiitthh MMiinnddffiirree ssiinnccee JJuullyy 11,,22001133 WWiinnddoowwss DDeesskkttoopp AApppplliiccaattiioonn DDeevveellooppeerr (CC##,, ..NNEETT)) SSkkyyppee:: mmffssii__kkaavveerriiss EEmmaaiill :: kkaavveerriiss@@mmiinnddffiirreessoolluuttiioonnss..ccoomm Presenter: Kaveri Saxena, Mindfire Solutions
  • 3. Presenter: Kaveri Saxena, Mindfire Solutions AGENDA ●Overview Of Dependency Injection ●Why Dependency Injection? ●Constructor Injection ●Setter Injection ●Interface Injection ●Overview of DI Containers ●Demo Implementing Dependency Injection with Unity Container ●Q&A
  • 4. Overview Of Dependency Injection Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them,whether at run time or compile time. Dependency injection is a set of software design principles and Pattern that enable us to develop loosely coupled codes. - Mark Seeman Tight Coupling Form (Class) Repository Var repository = new Repository();
  • 5. Why To Use DI????? ●Extensibility ●Testability ●Late Binding ●Parallel Development ●Maintainability
  • 6. Constructor Injection Pass dependency into dependent class via constructor var service = new SQLService(); Var form = new PersonForm(service); Application.Run(form); private IServiceRepository Repository; public PersonForm(IServiceRepository repository) { Repository = repository; }
  • 7. Setter Injection Create a setter on the dependent class and use that to set the dependency. var form = new PersonForm(); form.Repository = new CSVService(); Application.Run(form); private IServiceRepository Repository { get; set; }
  • 8. Interface Injection Dependent class implements an interface and injector uses the interface to set the dependency. var service = new SQLService(); var form = new PersonForm(); ((IInjectInterface)form).inject(service); Application.Run(form); public interface IInjectInterface { void inject(IServiceRepository serviceRepo); }
  • 9. public partial class PersonForm :Form,IInjectInterface { public void inject(IServiceRepository serviceRepo) { Repository = serviceRepo; } }
  • 10. Overview Of DI Container ●A framework for doing dependency Injection ●Configure dependencies ●Automatically resolves configured dependencies Eg. Unity,Ninject,,CastleWindsor,Autofac,structuremap, Spring.Net and many others. SqlService DI Container CSVService IService Person Form
  • 11. Unity Container Unity comes from microsoft pattern and practice team, available as free download. For using Unity Container- ●Download compatible version of unity in your bootstrapper Application ● Register interface with concrete type ●Call resolve method and unity will automatically find complex constructor of dependent class and use concrete type instead of interface. var container = new UnityContainer(); container.RegisterType<IServiceRepository, SQLService>(); var form = container.Resolve<PersonForm>(); Application.Run(form);
  • 12. Question and Answer Presenter: Kaveri Saxena, Mindfire Solutions
  • 13. Thank you Presenter: Kaveri Saxena, Mindfire Solutions