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

Dependency Injection with Unity Container

  • 1.
    Dependency Injection WithUnity Container Presenter: Kaveri Saxena, Mindfire Solutions Date: 28/04/2014
  • 2.
    KKaavveerrii SSaaxxeennaa WWoorrkkiinnggWWiitthh 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 DependencyInjection 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 UseDI????? ●Extensibility ●Testability ●Late Binding ●Parallel Development ●Maintainability
  • 6.
    Constructor Injection Passdependency 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 Createa 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 Dependentclass 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 classPersonForm :Form,IInjectInterface { public void inject(IServiceRepository serviceRepo) { Repository = serviceRepo; } }
  • 10.
    Overview Of DIContainer ●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 Unitycomes 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
  • 14.
  • 15.