Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | claribel-terry |
View: | 224 times |
Download: | 0 times |
UnderstandingDependency Injection… and those pesky containers
Miguel A. [email protected]@miguelcastro67
DEV-B207
ineta
Miguel A. Castro.NET Architect, Developer, & Trainer Microsoft MVP ASP Insider VSX Insider C# Insider Azure Insider Member of the INETA Speakers Bureau Conference Speaker In IT business since 1986
Agenda• Classes, Dependencies, and Coupling• DI Explained & Demoed• DI Containers• A Tour Through Several Containers• Unity,• NInject,• Castle Windsor• StructureMap• MEF
Agenda• Dependency Injection usage• WPF / Silverlight / WinRT• ASP.NET MVC• ASP.NET WebForms (yes, you read right)
Class coupling (why it’s bad)• One class depending on another• Cannot exist (compile) without other class
• Limits functionality to single implementation• If classes perform DB work, difficult to test
without hitting DB
To decouple, abstract• Program to interfaces (or abstract classes)• Hosting class uses interfaces• Can receive any implementation• Can have real one for production• Mocked one for testing
• Note: This is the part that makes “Testable Software” NOT use of a DI container !
demo
Essentials (coupled and abstracted)
What is Dependency Injection• An architectural pattern designed to easily
satisfy a class’ dependencies• Allows us to write decoupled code• Facilitate testability• Ease deployment of components
• Typically implemented with the aid of an object container
The DI Container• A repository for definitions typically relating an
abstraction to a concrete class• Core functionality• Provide facility for registering classes• Usually related to interfaces• Provide facility for resolving a request• Usually from a given interface (not always)
The DI Container• Type associations (registrations) achieved
depending on container• Procedural (fluent interface)• Unity, NInject, Castle Windsor, StructureMap• Configuration• Spring.NET• Declarative (attributes)• Managed Extensibility Framework (MEF)
The DI Container• Recursively resolves dependencies• Injected interface variables• Constructor (usual)• Property
• Requesting one class (from container) starts chain reaction
demo
DI Container internalsPoor-Man’s DI Container
Unity• Microsoft’s contribution to DI Containers• Code-based registration• XML-based registration• Constructor injection• Provides interception (aspects)• Part of Enterprise Library
NInject• Code-based registration• Attribute-based registration• Constructor injection
Castle Windsor• Second oldest DI container for .NET• XML-based registration• Code-based registration• One of the more popular DI containers
StructureMap• Oldest DI Container for .NET• Code-based registration• XML-based registration• Attribute-based registration
Spring.NET• Port from Spring for Java• XML-based registration (originally)• Code-based registration• Provides many additional features• AOP being a key one
• Lots of “business framework” functionality
MEF• Not a true DI container (so they tell me)• Serves purpose well though
• Framework for providing add-in architecture to application
• Provides features not found in DI containers
MEF• Uses discovery mechanism to find components
(good)• Only interfaces known at compile-time
• Components coupled to discovery mechanism (bad)• Attribute driven
• For most DI needs, works nicely• All, in my case
MEF• Discovery mechanism allows extra
customization• Can extend Export and Import attributes and alter discovery
behavior
• Include with .NET Framework
demo
DI Containers
Container ComparisonContainer Registration Injection Multiple
Constructors
Unity Programmatic, XML Constructor, Limited Property
Yes
NInject Programmatic Constructor, Limited Property
Yes
Castle Windsor Programmatic, XML Constructor, Property
Yes
StructureMap Programmatic, XML, Discovery
Constructor, Limited Property
Yes
Spring.NET Programmatic, XML Constructor, Property
Yes
MEF Discovery Constructor, Property
No
DI in WPF / Silverlight / WinRT• Used to resolve ViewModel classes• Used to resolve dependencies injected into ViewModels• Used to resolve nested ViewModels
• ViewModels can be tested and test dependency implementations used
DI in ASP.NET MVC• Used to resolve controller classes• Used to resolve injected dependencies into controllers
• Can use a custom controller factory• MVC offers dependency resolver• Controllers can be tested and test
dependency implementations used
DI in ASP.NET WebForms• Used to resolve injected dependencies into
requested web forms• Uses a custom page handler factory• Testing code-behind classes still troublesome
demo
DI Container Usage
Conclusion• DI Containers assist in managing
components to be used• The core principle is usage of interfaces to
build decoupled components• Containers offer different features• For most part, all accomplish the same thing
• MEF is first class citizen
References• Dependency Injection in .NET• Mark Seemann – Manning
• Martin Fowler’s DI/IoC article• http://martinfowler.com/articles/injection.html
• Tons of info on the web
msdn
Resources for Developers
http://microsoft.com/msdn
Learning
Microsoft Certification & Training Resources
www.microsoft.com/learning
TechNet
Resources
Sessions on Demand
http://channel9.msdn.com/Events/TechEd
Resources for IT Professionals
http://microsoft.com/technet
Complete an evaluation on CommNet and enter to win!
Evaluate this session
Scan this QR code to evaluate this session and be automatically entered in a drawing to win a prize
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.