1 / 43

Inversion Of Control & Dependency Injection

Inversion Of Control & Dependency Injection. Break Apart The Dependencies. Oren Eini Senior Developer We! Consulting Group http://www.ayende.com/Blog/. Who wants to try to move that?. What can we do about it?. Who am I and what I do?. Senior Developer at We!

ugo
Télécharger la présentation

Inversion Of Control & Dependency Injection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Who wants to try to move that?

  3. What can we do about it?

  4. 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 LazyDeveloper

  5. 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

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

  7. A bit about terminology • Container • Service / Component • Inversion of Control • Dependency Injection

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

  9. Let us have a pizza…

  10. The Pizza Process

  11. Coupled Pizza Place

  12. Client Code Customercustomer = newCustomer("Fred"); CoupledPizzaPlacepizzaPlace = newCoupledPizzaPlace(); pizzaPlace.MakeOrder(customer,3);

  13. Clerk knows about cook • Deliveries service knows about clerk • Cook knows about clerk Coupled Pizza Place Help the cook trade jobs. Help the clerk take vacation.

  14. Coupled Pizza Place

  15. Just put some interfaces… Decoupled Pizza Place

  16. Decoupled Client Code Ovenoven = newOven(); MakePizzaCooktakePizzaFromOvenCook = newMakePizzaCook(oven); OrdersClerkordersClerk = newOrdersClerk(takePizzaFromOvenCook); MakeDevliveriesServicemakeDevliveriesService = newMakeDevliveriesService(ordersClerk); TakePizzaFromOvenCookmakePizzaCook = newTakePizzaFromOvenCook(oven, ordersClerk); DecoupledPizzaPlacepizzaPlace = newDecoupledPizzaPlace(takePizzaFromOvenCook, ordersClerk, makeDevliveriesService, makePizzaCook); Customercustomer = newCustomer("Fred"); pizzaPlace.MakeOrder(customer,3);

  17. Into Factory Method DecoupledPizzaPlacepizzaPlace = DecoupledPizzaPlace.Create(); Customercustomer = newCustomer("Fred"); pizzaPlace.MakeOrder(customer, 3); Did we really lower the cost of change?

  18. Decoupled Pizza Place

  19. IoC Pizza Place

  20. IoC Client Code When Application Starts IoC.Initialize(newRhinoContainer("Pizza.boo")); Customercustomer = newCustomer("Fred"); IoCPizzaPlacepizzaPlace = IoC.Resolve<IoCPizzaPlace>(); pizzaPlace.MakeOrder(customer, 3); pizzaPlace.DoWork(); Configuration(later)

  21. Configuring IoC: Using DSL Yes, you can do it with XML as well… Component("sara", IClerk, OrdersClerk) Component("nissim", ICook, MakePizzaCook) Component("moshe", IMakeDeliveries, MakeDeliveriesService) Component("nir", ICook, TakePizzaFromOvenCook) Component("oven", Oven) Component("pizza", IoCPizzaPlace, secondCook: @nir)

  22. IoC.Resolve<IoCPizzaPlace>(); IClerk ICook √ √ √ √ ICook √ √ IMakeDeliveries √ IClerk √ √ IClerk √ Second Cook ICook √ √ Oven √

  23. 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.

  24. 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

  25. Why not do it myself?

  26. Flexibility… , cook: @nir); TempClerk ); cook: @nir, secondCook: @nissim) Component("sara", IClerk, OrdersClerk) Component("nissim", ICook, MakePizzaCook) Component("moshe", IMakeDeliveries, MakeDeliveriesService) Component("nir", ICook, TakePizzaFromOvenCook) Component("oven", Oven) Component("pizza", IoCPizzaPlace, secondCook: @nir);

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

  28. Implementations In .Net • Castle Windsor • Spring .Net • Structure Map • Object Builder

  29. Advance Stuff:Exploding heads zone

  30. Generic Services Usage: //from database IoC.Resolve<IRepository<Customer>>.Get(1337); //from active directory IoC.Resolve<IRepository<User>>.Get(42);

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

  32. Generic Specialization Usage: IoC.Resolve<IRepository<User>>.Get(5); IoC.Resolve<IRepository<Customer>>.Get(15);

  33. A word about decorators

  34. 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)

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

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

  37. Cons • Need to learn • Higher level of abstraction • Misuse of architecture

  38. 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

  39. Questions? Come to meet me at theArchitecture & Developers Panels or visit my blog: http://www.ayende.com/Blog

  40. המשוב שלכם חשוב לנו!

  41. המשוב שלכם חשוב לנו! { לא לשכוח למלא משוב מסכם על כל היום! }

  42. Thank You!

More Related