Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group"— Presentation transcript:

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

2

3 Who wants to try to move that?

4 What can we do about it?

5 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

6 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

7 What is it – Technology? (Very) smart factory Automatic resolving and injection of dependencies

8 A bit about terminology Container Service / Component Inversion of Control Dependency Injection

9 What is the problem we are trying to solve? To understand the solution, we need to understand what the problem is…

10 LET US HAVE A PIZZA…

11 The Pizza Process

12 Coupled Pizza Place

13 Client Code Customer customer = new Customer("Fred"); CoupledPizzaPlace pizzaPlace = new CoupledPizzaPlace(); pizzaPlace.MakeOrder(customer,3);

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

15 Coupled Pizza Place

16 Decoupled Pizza Place Just put some interfaces…

17 Decoupled Client Code Oven 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);

18 Into Factory Method DecoupledPizzaPlace pizzaPlace = DecoupledPizzaPlace.Create(); Customer customer = new Customer("Fred"); pizzaPlace.MakeOrder(customer, 3);

19 Decoupled Pizza Place

20 IoC Pizza Place

21 IoC Client Code IoC.Initialize( new RhinoContainer("Pizza.boo")); Customer customer = new Customer("Fred"); IoCPizzaPlace pizzaPlace = IoC.Resolve (); pizzaPlace.MakeOrder(customer, 3); pizzaPlace.DoWork(); Configuration (later)

22 Configuring IoC: Using DSL Component("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…

23 IoC.Resolve (); IClerk IMakeDe liveries ICook IClerk Oven Second Cook

24 Inversion Of Control Container All the services in the application 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.

25 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

26 Why not do it myself?

27 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);

28 And in the real world… Changing the payment service: PayPal  Credit Card Changing the delivery service: UPS  FedEx Changing a database Replacing validation rules

29 Implementations In.Net Castle Windsor Spring.Net Structure Map Object Builder

30 ADVANCE STUFF: EXPLODING HEADS ZONE

31 Generic Services Usage: //from database IoC.Resolve >.Get(1337); //from active directory IoC.Resolve >.Get(42);

32 Configuration Component("users_repository", IRepository of User, ActiveDirectoryRepository) Component("database_repository", IRepository, NHibernateRepository)

33 Generic Specialization Usage: IoC.Resolve >.Get(5); IoC.Resolve >.Get(15);

34 A word about decorators Client code… Security Decorator Logging Decorator Caching Decorator Real Reposistory

35 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)

36 Other uses of Inversion of Control Containers Manage lifetime of services (singleton, transient, per request, etc). Aspect Oriented Programming Decorators

37 Pros Simpler Architecture Reduced cost of change Encourage best practices Interface driven design and component oriented programming Less work More smarts from the framework

38 Cons Need to learn Higher level of abstraction Misuse of architecture

39 Resources Inversion of Control Containers and the Dependency Injection pattern – Martin Fowler Inversion of Control Containers and the Dependency Injection pattern The Dependency Injection Principal - Robert C. Martin The Dependency Injection Principal Inversion of Control and Dependency Injection: Working with Windsor Container – Oren Eini Inversion of Control and Dependency Injection: Working with Windsor Container

40 Come to meet me at the Architecture & Developers Panels or visit my blog: http://www.ayende.com/Blog

41

42

43


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

Similar presentations


Ads by Google