Inversion of Control and Dependency Injection
Upcoming SlideShare
Loading in...5
×
 

Inversion of Control and Dependency Injection

on

  • 2,078 views

This slide describes that how Dependency injection works for MVC 4 Web API.

This slide describes that how Dependency injection works for MVC 4 Web API.

Statistics

Views

Total Views
2,078
Views on SlideShare
2,078
Embed Views
0

Actions

Likes
0
Downloads
40
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Inversion of Control and Dependency Injection Inversion of Control and Dependency Injection Presentation Transcript

  • Inversion of control andDependency Injection By Dinesh Sharma
  •  Aware of MVC Framework Aware of Web API
  •  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.
  •  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.
  •  The greatest benefit is that it encourages dependency free architecture. Your classes stand alone, and do not depend on a specific implementation (other than its 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. Continue ……
  •  Code becomes more reusable, testable and readable. When you test something, if it has a hard coded new in there, you cant 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.
  •  We can inject ◦ Controllers ◦ Views ◦ Constructors ◦ Filters
  •  Unity (Microsoft Enterprise Library block) Ninject Castle Windsor Autofac StructureMap Spring.Net Etc…….
  •  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. public class EmployeeController : ApiController { private readonly EmployeeRepository repository; public EmployeeController() { repository = new EmployeeRepository(); …………… Products Products API Repository
  •  If we create the interface for ProductRepository then we can hide our implementation as below – public class ProductsController : ApiController { private readonly IProductRepository repository; public ProductsController() { this.repository = new ProductRepository(); } IEmployee Repository Employee Employee API Repository
  •  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)
  •  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…….
  •  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); } }
  •  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.
  • class ScopeContainer : IDependencyScope{ protected IUnityContainer container; public ScopeContainer(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; } Continued…
  • //Creates one instance of a specified typepublic object GetService(Type serviceType){ if container.IsRegistered(serviceType)) { return container.Resolve(serviceType); } else { return null; }} Continued…
  • //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(); }
  •  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);}
  • Now inject the Dependency, which isEmployeeRepository here…….public class EmployeeController : ApiController { private readonly IEmployeeRepository repository; public EmployeeController(IEmployeeRepository repository ) { if (repository == null) { throw new ArgumentNullException("repository"); } this.repository = repository; }
  •  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.net-mvc.aspx
  • By Dinesh Sharma