170 likes | 352 Views
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
E N D
Introduction to Dependency Injection & IoC Containers Get where you’re going. Roberto HernándezManaging ConsultantExcella Consulting
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
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
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
Object Structure - Traditional • Entity • Database Access Service • Service Proxy • Logging Service • Etc.. Database Access Service Service Proxy Logging Service Tightly coupled services!
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
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).
Quotes • “Dependency Injection is a key element of an agile architecture.” • Ward Cunningham
Impl DI - Using DI & IoC Container • What are our options? • StructureMap • Ninject • LinFu • Unity • Castle.Windsor • Spring.NET • …
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.
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
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.
Recap • Blog: http://www.OverrideThis.com • Twitter: hernandezrobert • Email: • roberto.hernandez@excella.com