ineta .net architect, developer, & trainer microsoft mvp asp insider vsx insider c# insider ...

34

Upload: claribel-terry

Post on 23-Dec-2015

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers
Page 2: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

UnderstandingDependency Injection… and those pesky containers

Miguel A. [email protected]@miguelcastro67

DEV-B207

Page 3: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 4: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Agenda• Classes, Dependencies, and Coupling• DI Explained & Demoed• DI Containers• A Tour Through Several Containers• Unity,• NInject,• Castle Windsor• StructureMap• MEF

Page 5: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Agenda• Dependency Injection usage• WPF / Silverlight / WinRT• ASP.NET MVC• ASP.NET WebForms (yes, you read right)

Page 6: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 7: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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 !

Page 8: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

demo

Essentials (coupled and abstracted)

Page 9: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 10: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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)

Page 11: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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)

Page 12: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

The DI Container• Recursively resolves dependencies• Injected interface variables• Constructor (usual)• Property

• Requesting one class (from container) starts chain reaction

Page 13: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

demo

DI Container internalsPoor-Man’s DI Container

Page 14: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Unity• Microsoft’s contribution to DI Containers• Code-based registration• XML-based registration• Constructor injection• Provides interception (aspects)• Part of Enterprise Library

Page 15: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

NInject• Code-based registration• Attribute-based registration• Constructor injection

Page 16: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Castle Windsor• Second oldest DI container for .NET• XML-based registration• Code-based registration• One of the more popular DI containers

Page 17: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

StructureMap• Oldest DI Container for .NET• Code-based registration• XML-based registration• Attribute-based registration

Page 18: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 19: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 20: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 21: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

MEF• Discovery mechanism allows extra

customization• Can extend Export and Import attributes and alter discovery

behavior

• Include with .NET Framework

Page 22: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

demo

DI Containers

Page 23: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 24: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 25: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 26: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 27: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

demo

DI Container Usage

Page 28: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 29: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 30: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 31: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Complete an evaluation on CommNet and enter to win!

Page 32: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Evaluate this session

Scan this QR code to evaluate this session and be automatically entered in a drawing to win a prize

Page 33: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

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

Page 34: ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers

Miguel A. Castro

Thank You !@miguelcastro67

www.dotnetdude.com

[email protected]