Top Banner
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group http://www.ayende.com/ Blog/
43

Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Jan 02, 2016

Download

Documents

Scot Horton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Inversion Of Control& Dependency Injection

Break Apart The DependenciesOren EiniSenior DeveloperWe! Consulting Grouphttp://www.ayende.com/Blog/

Page 2: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Page 3: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Who wants to try to move that?

Page 4: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

What can we do about it?

Page 5: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Who am I and what I do?• Senior Developer at We!• Mostly dealing with complex business

applications• 100% of current projects using IoC• Blogger - ~95,000 visits / month– Object Relational Mapping– Inversion Of Control– Model View Controller architectures for the web– Various Development topics

Lazy

Developer

Page 6: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

What it is – Architecture?

• Decreased Coupling• Greater Flexibility• Separation of Concerns • Interface Driven Design • Dependency Management (via Dependency

Injection or Service Lookup) • Component Oriented Programming

Reduced Cost Of Change

Page 7: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

What is it – Technology?

• (Very) smart factory• Automatic resolving and injection of

dependencies

Page 8: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

A bit about terminology

• Container• Service / Component• Inversion of Control• Dependency Injection

Page 9: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

What is the problem we are trying to solve?

• To understand the solution, we need to understand what the problem is…

Page 10: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

LET US HAVE A PIZZA…

Page 11: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

The Pizza Process

Page 12: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Coupled Pizza Place

Page 13: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Client Code

Customer customer = new Customer("Fred");

CoupledPizzaPlace pizzaPlace = new CoupledPizzaPlace();

pizzaPlace.MakeOrder(customer,3);

Page 14: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Coupled Pizza Place•Clerk knows about cook•Deliveries service knows about clerk•Cook knows about clerk

HELP THE COOK TRADE JOBS.

HELP THE CLERK TAKE VACATION.

Page 15: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Coupled Pizza Place

Page 16: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Decoupled Pizza PlaceJust put some

interfaces…

Page 17: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Decoupled Client CodeOven oven = new Oven();

MakePizzaCook takePizzaFromOvenCook = new MakePizzaCook(oven);

OrdersClerk ordersClerk = new OrdersClerk(takePizzaFromOvenCook);

MakeDevliveriesService makeDevliveriesService = new MakeDevliveriesService(ordersClerk);

TakePizzaFromOvenCook makePizzaCook = new TakePizzaFromOvenCook(oven, ordersClerk);

DecoupledPizzaPlace pizzaPlace =

new DecoupledPizzaPlace(takePizzaFromOvenCook, ordersClerk, makeDevliveriesService, makePizzaCook);

Customer customer = new Customer("Fred");

pizzaPlace.MakeOrder(customer,3);

Page 18: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Into Factory Method

DecoupledPizzaPlace pizzaPlace =

DecoupledPizzaPlace.Create();

Customer customer = new Customer("Fred");

pizzaPlace.MakeOrder(customer, 3);

Did we really lower the cost of change?

Page 19: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Decoupled Pizza Place

Page 20: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

IoC Pizza Place

Page 21: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

IoC Client Code

IoC.Initialize(new RhinoContainer("Pizza.boo"));

Customer customer = new Customer("Fred");

IoCPizzaPlace pizzaPlace = IoC.Resolve<IoCPizzaPlace>();

pizzaPlace.MakeOrder(customer, 3);

pizzaPlace.DoWork();

Configuration(later)

When Application Starts

Page 22: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Configuring IoC: Using DSLComponent("sara", IClerk, OrdersClerk)Component("nissim", ICook, MakePizzaCook)Component("moshe", IMakeDeliveries,

MakeDeliveriesService)Component("nir", ICook, TakePizzaFromOvenCook)Component("oven", Oven)Component("pizza", IoCPizzaPlace,

secondCook: @nir)

Yes, you can do it with XML as

well…

Page 23: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

IoC.Resolve<IoCPizzaPlace>();

IClerk

IMakeDeliveries

ICook

ICook

ICook

IClerk

IClerk

Oven

Second Cook

√√ √ √

√√

Page 24: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Inversion Of Control Container

• All the services in theapplication are registered in the container.

• Single point of access to all the services in the application.

• Automatically resolves all the dependencies of services registered with the container.

• Neither the client nor the service are tied to the dependencies.

Page 25: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Benefits of IoC

• Dependencies are managed for you.• Highly focused objects (single responsibility,

separation of concerns).• Testability.• Objects are not coupled directly to

environment resources or other unintended implementations

Page 26: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Why not do it myself?

Managing dependencie

s automaticall

y

Managing dependencies in a single

place

Managing life cycle for all objects

Easy Customizatio

n

Page 27: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Flexibility…Component("sara", IClerk,

OrdersClerk)

Component("nissim", ICook, MakePizzaCook)Component("moshe", IMakeDeliveries,

MakeDeliveriesService)Component("nir", ICook, TakePizzaFromOvenCook)Component("oven", Oven)Component("pizza", IoCPizzaPlace,

secondCook: @nir);

TempClerk;)

cook: @nir, secondCook: @nissim)

,cook: @nir;)

Page 28: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

And in the real world…

• Changing the payment service: PayPal Credit Card

• Changing the delivery service: UPS FedEx• Changing a database• Replacing validation rules

Page 30: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

ADVANCE STUFF:EXPLODING HEADS ZONE

Page 31: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Generic Services

Usage://from databaseIoC.Resolve<IRepository<Customer>>

.Get(1337);//from active directoryIoC.Resolve<IRepository<User>>

.Get(42);

Page 32: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Configuration

Component("users_repository", IRepository of User, ActiveDirectoryRepository)

Component("database_repository", IRepository, NHibernateRepository)

Page 33: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Generic SpecializationUsage:IoC.Resolve<IRepository<User>>

.Get(5);IoC.Resolve<IRepository<Customer>>

.Get(15);

Page 34: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

A word about decorators

Client code…

Page 35: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Configuration

Component("logging_users_repository", IRepository of User, LoggingDecorator of User,

inner: @users_repository)

Component("logging_ database _repository", IRepository, LoggingDecorator,

inner: @database_repository)

Component("users_repository", IRepository of User, ActiveDirectoryRepository)

Component("database_repository", IRepository, NHibernateRepository)

Page 36: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Other uses of Inversion of Control Containers

• Manage lifetime of services (singleton, transient, per request, etc).

• Aspect Oriented Programming• Decorators

Page 37: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Pros• Simpler Architecture• Reduced cost of change• Encourage best practices• Interface driven design and component oriented

programming• Less work• More smarts from the framework

Page 38: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Cons

• Need to learn• Higher level of abstraction• Misuse of architecture

Page 39: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Resources

• Inversion of Control Containers and the Dependency Injection pattern – Martin Fowler

• The Dependency Injection Principal - Robert C. Martin• Inversion of Control and Dependency Injection: Working with

Windsor Container – Oren Eini

Page 40: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Questions?

Come to meet me at the Architecture & Developers

Panels or visit my blog:http://www.ayende.com/Blog

Page 41: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

המשוב שלכם

! לנו חשוב

Page 42: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

למלא} לשכוח לאכל על מסכם משוב

היום! {

המשוב שלכם

! לנו חשוב

Page 43: Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group

Thank You!