Inversion of Control in MVC

Preview:

Citation preview

Inversion of Control (IoC)in

ASP.NET MVC

C# Corner-Delhi ChapterNOV 7, 2015

IoC?

DI?

MVC?

What we are going to talk about:

• How to Invert the Control of Dependencies

…in ASP.NET MVC

What we are going to talk about:

• “How to Invert the Control of Dependencies in ASP.NET MVC”

SOLID PrinciplesS : Single Responsibility PrincipleO : Open/Closed PrincipleL : Liskov Substitution PrincipleI : Interface Segregation PrincipleD : Dependency Injection

D : Dependency Injection“Depend upon Abstractions. DO NOT depend on Concrete Implementations”

Dependency Injection• Constructor Injection• Setter Injection• Interface Injection

IoC (Inversion of Control)- Isn’t a design pattern- Is a process of inverting the

control- Helps implementing Dependency

Injection

Inversion of Control• Interface Inversion• Flow Inversion• Creation Inversion

IoC ContainerDependency A

IoC Container

Dependency B

IDependency

MyClass

IoC ContainerMasterCard

IoC Container

Visa

ICreditCard

Shopper

without IoC

Battery

Wall Outlet

Solar Cell

Power Source

Laptop

IoC Container

Laptop

Mapping

IPowerSource ->Battery…..…..

With IoC

Battery

Route Handler

ASP.NET MVC Request

View

Request

Controller

Model

MvcHandler

How Controllers are created

Controller

Request

ControllerFactory

IControllerFactory

DefaultControllerFactory

Let’s build a Custom Controller Factory!

So many flavors… which one to choose?

Ninject

Unity

AutoFac Funq

LinFu

Spring.Net Windsor

StructureMapMany more…

< me />Sunny Sharma• Trainer / Author / Speaker

• corp.sunnykumar@gmail.com

• Twitter : @sunny_delhi

Thank You!

Recommended