SlideShare a Scribd company logo
1 of 15
Download to read offline
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

What's hot

Agile methodologies based on BDD and CI by Nikolai Shevchenko
Agile methodologies based on BDD and CI by Nikolai ShevchenkoAgile methodologies based on BDD and CI by Nikolai Shevchenko
Agile methodologies based on BDD and CI by Nikolai Shevchenko
Moldova ICT Summit
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805
LearningTech
 

What's hot (20)

MongoDB Stitch Tutorial
MongoDB Stitch TutorialMongoDB Stitch Tutorial
MongoDB Stitch Tutorial
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"
Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"
Vasyl Aleksashyn "How to Stop Shooting Yourself in the Foot"
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
Agile methodologies based on BDD and CI by Nikolai Shevchenko
Agile methodologies based on BDD and CI by Nikolai ShevchenkoAgile methodologies based on BDD and CI by Nikolai Shevchenko
Agile methodologies based on BDD and CI by Nikolai Shevchenko
 
Jasig Cas High Availability - Yale University
Jasig Cas High Availability -  Yale UniversityJasig Cas High Availability -  Yale University
Jasig Cas High Availability - Yale University
 
Distributing information on iOS
Distributing information on iOSDistributing information on iOS
Distributing information on iOS
 
Deployment
DeploymentDeployment
Deployment
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programming
 
Ngrx meta reducers
Ngrx meta reducersNgrx meta reducers
Ngrx meta reducers
 
Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)
 
Proxy design pattern
Proxy design patternProxy design pattern
Proxy design pattern
 
CDI @javaonehyderabad
CDI @javaonehyderabadCDI @javaonehyderabad
CDI @javaonehyderabad
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805
 
Functional programming in C#
Functional programming in C#Functional programming in C#
Functional programming in C#
 
Introduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphereIntroduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphere
 
Proxy Pattern
Proxy PatternProxy Pattern
Proxy Pattern
 

Viewers also liked

Viewers also liked (10)

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

Similar to Dependency Injection with Unity Container

Dependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call youDependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call you
Dmytro Mindra
 
JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...
JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...
JavaOne 2013 BOF 3861 - Mixing OAuth 2.0, Jersey and Guice to Build an Ecosys...
Matthias Miltz
 
Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...
Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...
Mixing OAuth 2.0, Jersey and Guice to Build an Ecosystem of Apps - JavaOne...
Hermann Burgmeier
 

Similar to Dependency Injection with Unity Container (20)

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

More from Mindfire Solutions

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 

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