160 likes | 242 Vues
Dealing with dependencies. jon fuller. IoC , DI, DIP, OMG, WTF?. Acronym overload! Whats it all mean? IoC – Inversion of Control DI – Dependency Injection DIP – Dependency Inversion Principle Often used interchangeably, but we’ll see they aren’t really all the same thing. IoC.
E N D
Dealing with dependencies jon fuller
IoC, DI, DIP, OMG, WTF? Acronym overload! Whats it all mean? • IoC – Inversion of Control • DI – Dependency Injection • DIP – Dependency Inversion Principle Often used interchangeably, but we’ll see they aren’t really all the same thing.
IoC • Declarative – Separation of what to do and how to do it • Hollywood Principle • Examples: • Winforms/WPF • Ruby On Rails • Dependency Injection
DI[P] • Uncle Bob Says High-level modules should not depend on low-level modules. Both should depend on abstractions. • Dependencies are supplied by the caller of a module • The D in SOLID (saved the best for last? nah.)Single Responsibility PrincipleOpen Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle source
But Why? • It’s Awesome! • All the cool kids are doing it! (read: Fowler, Uncle Bob, Me (did you know Cool is my middle name?), etc.) • Simplicity • Testability* • Configurability source * Loaded Term… Watch out!
Example – Non-DI Tightly coupled!
Example – DI Injected Dependency Injection site
Yeah, but… You: Someone has to create that dependency right? Me: Yep You: Ah, so we just push it up one level. Wait, where’s the benefit in that? Me: Enter Container. source
Example – DI w/Container Configure Container Get instance from container
Example – DI w/Container (Autowire) Register the finder Configure finder Autowire, Automagic
Conventions/Features • Constructor Injection • Property Injection • Lifecycle Management • AOP/Interception
Common Implementations • .NET • Windsor • StructureMap • Unity • Ninject • AutoFac • Spring.NET • Java • Spring • Nano/Pico Container • Tapestry/Hivemind • Ruby • Copland
Configuration Management • Fluent (StructureMap) • DSL (Binsor) • XML(Windsor)
Testability • Loosely Coupled/Highly Cohesive • TDD • Mocking
Resources • Fowler • http://martinfowler.com/articles/injection.html • http://martinfowler.com/bliki/InversionOfControl.html • Uncle Bob • www.objectmentor.com/resources/articles/dip.pdf • StructureMap – http://structuremap.sourceforge.net/ • Windsor – http://www.castleproject.org • Unity – http://www.codeplex.com/unity • Common Service Locator – http://www.codeplex.com/CommonServiceLocator
Questions?/Comments/Feedback source