![Page 1: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/1.jpg)
Inversion of control and
Dependency Injection
By Dinesh Sharma
![Page 2: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/2.jpg)
Aware of MVC Framework Aware of Web API
Assumption
![Page 3: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/3.jpg)
Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.
It means, that in procedural programming a chunk of code that uses, or consumes, another chunk of code is in control of the process.
What is Inversion of Control (IoC) principle ….
![Page 4: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/4.jpg)
Dependency Injection is a great way to reduce tight coupling between software components.
Instead of hard-coding dependencies, such as specifying a database driver, you inject a list of services that a component may need. The services are then connected by a third party.
This technique enables you to better manage future changes and other complexity in your software.
What is Dependency Injection
![Page 5: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/5.jpg)
The greatest benefit is that it encourages dependency free architecture. Your classes stand alone, and do not depend on a specific implementation (other than it's interface and requirements)
Flexibility to use alternative implementation of a given service without recompiling the application code.
IoC container can control the lifetime of your dependencies. Suppose you want an object to exist for the lifetime of the request in web page.
Why Dependency Injection…
Continue……
![Page 6: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/6.jpg)
Code becomes more reusable, testable and readable.
When you test something, if it has a hard coded new in there, you can't easily replace it with a mocked object to feed it test data. Suppose you want to test that an object calculates a value correctly. Feeding it test data with known values makes it easier to test.
Why …..
![Page 7: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/7.jpg)
We can inject ◦ Controllers◦ Views◦ Constructors◦ Filters
Where can be used for an MVC application….
![Page 8: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/8.jpg)
Unity (Microsoft Enterprise Library block) Ninject Castle Windsor Autofac StructureMap Spring.Net Etc…….
IoC Containers
![Page 9: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/9.jpg)
We have Products API, which implement the Products repository. So we need to create an object of Product Repository to access its functionality into API.
Applying IoC – A Scenario
Products APIProducts
Repository
public class EmployeeController : ApiController { private readonly EmployeeRepository repository;
public EmployeeController() { repository = new EmployeeRepository();……………
![Page 10: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/10.jpg)
If we create the interface for ProductRepository then we
can hide our implementation as below –
Applying IoC – A Scenario
public class ProductsController : ApiController { private readonly IProductRepository repository;
public ProductsController() {
this.repository = new ProductRepository(); }
Employee API
Employee Repository
IEmployee Repository
![Page 11: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/11.jpg)
In this scenario still we need to look into the implementation of ProductRepository.
How to avoid dependency of ProductRepository?
Let’s inject the Dependency using IoC (Unity)
Applying IoC – A Scenario
![Page 12: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/12.jpg)
Create MVC 4 WebAPI project Using NuGet install package for Unity
◦ Install-package Unity Configure IoC Container (as in coming up
slides)
and ready to Go…….
How to start…..
![Page 13: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/13.jpg)
IoC container implements the Scope container and IDependencyResolver interface, which required to implement the BeginScope() method as below -
class IoCContainer : ScopeContainer, IDependencyResolver
{
public IoCContainer(IUnityContainer container) : base(container)
{
}
//Creates a nested scope.
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new ScopeContainer(child);
}
}
Create IoC Container class…
![Page 14: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/14.jpg)
Now, Let’s write the ScopeContainer class, which implements the IDependencyScope interface. IDependencyScope having two methods GeteService() and GetServices(),
These two methods are responsible to create the object of dependent class.
Create IoC Container class…
![Page 15: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/15.jpg)
class ScopeContainer : IDependencyScope { protected IUnityContainer container;
public ScopeContainer(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; }
Implementation…
Continued…
![Page 16: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/16.jpg)
//Creates one instance of a specified typepublic object GetService(Type serviceType){
if container.IsRegistered(serviceType)) {
return container.Resolve(serviceType);}else
{return null;
}}
Implementation…
Continued…
![Page 17: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/17.jpg)
//Create a collection of objects of a specified type public IEnumerable<object> GetServices(Type serviceType) { if (container.IsRegistered(serviceType)) { return container.ResolveAll(serviceType); } else { return new List<object>(); } }//Once process is done container dispose the objects public void Dispose() { container.Dispose(); }
Implementation…
![Page 18: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/18.jpg)
Let’s create the container and register our dependencies to this –
void ConfigureApi(HttpConfiguration config){
var unity = new UnityContainer();unity.RegisterType<EmployeeController>();unity.RegisterType<IEmployeeRepository,
EmployeeRepository>(new HierarchicalLifetimeManager());config.DependencyResolver = new
IoCContainer(unity);}
Initial calls…
![Page 19: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/19.jpg)
Now inject the Dependency, which is EmployeeRepository here…….
public class EmployeeController : ApiController { private readonly IEmployeeRepository repository;
public EmployeeController(IEmployeeRepository repository ) { if (repository == null) { throw new ArgumentNullException("repository"); } this.repository = repository; }
All set….. Let’s implement
![Page 20: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/20.jpg)
Download Unity - http://www.microsoft.com/en-in/download/details.aspx?id=17866 References –
◦ http://netmvc.blogspot.in/2012/04/dependency-injection-in-aspnet-mvc-4.html◦ http://haacked.com/archive/2007/12/07/tdd-and-dependency-injection-with-asp.ne
t-mvc.aspx
References
![Page 21: Inversion of Control and Dependency Injection](https://reader035.vdocuments.mx/reader035/viewer/2022062704/5562f924d8b42a275f8b48a8/html5/thumbnails/21.jpg)
Inversion of control and
Dependency Injection
By Dinesh Sharma