1 / 15

Introduction to Dependency Injection & IoC Containers

Introduction to Dependency Injection & IoC Containers. Get where you’re going. Roberto Hernández Managing Consultant Excella Consulting. Agenda. Object Structure Traditional Object Structure With Dependency Injection & IoC Containers Benefits of using DI & IoC Containers

didier
Download Presentation

Introduction to Dependency Injection & IoC Containers

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. Introduction to Dependency Injection & IoC Containers Get where you’re going. Roberto HernándezManaging ConsultantExcella Consulting

  2. Agenda • Object Structure • Traditional Object Structure • With Dependency Injection & IoC Containers • Benefits of using DI & IoC Containers • Implementing DI • By applying the Factory Pattern • By using and DI & IoC Container • Demos

  3. What is DI? • Dependency injection (DI) in object-orientedcomputer programming is a technique for supplying an external dependency (i.e. a reference) to a software component - that is, indicating to a part of a program which other parts it can use. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency. The term was first coined by Martin Fowler to describe the mechanism more clearly http://en.wikipedia.org/wiki/Dependency_injection

  4. What is an IoC? • 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. • There is a decoupling of the execution of a certain task from implementation. • Every system can focus on what it is designed for. • Every system does not make assumptions about what other systems do or should do. • Replacing systems will have no side effect on other systems. http://en.wikipedia.org/wiki/Inversion_of_control

  5. Object Structure - Traditional • Entity • Database Access Service • Service Proxy • Logging Service • Etc.. Database Access Service Service Proxy Logging Service Tightly coupled services!

  6. Object Structure - Traditional • Pros • Very ease to read. • Cons • Doesn’t promote reusability • Difficult to “mock” makes testing difficult • Creates brittle systems #Fail #Fail #Fail #Fail

  7. Object Structure - Design w/DI & IoC

  8. Benefit of using DI & IoC • Assists in implementing real world solutions that adhere to the SOLID principles • (SRP) The Single Responsibility Principle • (OCP) The Open Closed Principle • (LSP) The Liskov Substitution Principle • (ISP) The Interface Segregation Principle • (DIP) The Dependency Inversion Principle • Assists in building a more flexible environment for practicing Test Driven Development (TDD).

  9. Quotes • “Dependency Injection is a key element of an agile architecture.” • Ward Cunningham

  10. Impl DI – Factory Pattern

  11. Impl DI - Using DI & IoC Container • What are our options? • StructureMap • Ninject • LinFu • Unity • Castle.Windsor • Spring.NET • …

  12. Impl DI - Using DI & IoC Container • What does a DI & IoC Container provide? • Configuration API’s. • Injection for setters and constructor dependencies. • Lifecycle management • Singleton • Unique per process • Http Session • Http Request • Auto-mocking capabilities.

  13. Demo 1 – StructureMap • 01 - Walkthrough of Application • 02 - Refactoring with DI & IoC in mind. • 03 - Poor man’s DI. • 04 - StructureMap Configuration API’s • Simple Configuration, and Registries. • 05 - Making the Application aware of StructureMap • 06 – Taking advantage of • Lifetime Management • Interception

  14. Review • Dependency Injection & IoC Containers are Enterprise Architecture components that add: • Flexibility to your application. • Promote “Separation of Concerns”. • If you practice TDD, BDD, or even TAD in .NET and are not using DI & IoC Containers you are probably doing too much work to make it happen.

  15. Recap • Blog: http://www.OverrideThis.com • Twitter: hernandezrobert • Email: • roberto.hernandez@excella.com

More Related