introduction to the managed extensibility framework in silverlight

24
wintellect.com consulting training design debugging Intro to MEF with Silverlight Jeremy Likness Project Manager, Senior Consultant [email protected] Copyright © 2011

Upload: jeremy-likness

Post on 18-Dec-2014

970 views

Category:

Technology


2 download

DESCRIPTION

Learn how to use the Managed Extensibility Framework in Silverlight.

TRANSCRIPT

Page 1: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

Intro to MEF with Silverlight

Jeremy LiknessProject Manager, Senior [email protected] Copyright © 2011

Page 2: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

Founded by top experts on Microsoft – Jeffrey Richter, Jeff Prosise, and John Robbins – we pull out all the stops to help our customers achieve their goals through advanced software-based consulting and training solutions.

Consulting & Debugging• Architecture, analysis, and design services• Full lifecycle custom software development• Content creation• Project management• Debugging & performance tuning

Training• On-site instructor-led training• Virtual instructor-led training• Devscovery conferences

Design• User Experience Design• Visual & Content Design• Video & Animation Production

what we do

who we are

how we do it

consulting training debuggingdesign

Page 3: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Why MEF?• Discovery• Lifetime Management• Extensibility• Metadata• 10 Practical Reasons to use

MEF

Agenda

Page 4: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Misconception: Managed Extensibility Framework

• Part of the framework• Discovery• Lifetime management• Extensibility• Metadata

Why MEF?

Page 5: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• I admit I am a MEF addict! • You may be surprised by who has

done MEF• Visual Studio 2010• 2010 Vancouver Olympics• Microsoft (Looking Glass)• SharePoint

Welcome to the MEF Lab!

Page 6: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Import: Contract and Demand• Export: Implementation and Supply• Part: Supply & Demand

Discovery

[Import]public IMessage MyMessenger { get; set; }

[Export(typeof(IMessage))]public class SpecificMessenger : IMessage

“I need something that implements IMessage”

“I have something that implements IMessage”

Look Ma! I don’t need

500 constructor overloads!

Page 7: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Catalogs tell MEF where to find parts• AssemblyCatalog: Current assembly• TypeCatalog: List of types • DeploymentCatalog: Silverlight XAP

file• AggregateCatalog: Composed of

Multiple Catalogs

Discovery: Catalogs

Page 8: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• The Container wraps the Catalog• The Container is responsible for

Composition:– What parts exist?

• What imports exist for a part?• What exports exist for a part?

• This is the Discovery step• In Silverlight specifically the

“CompositionInitializer” handles containers

Discovery: Container

Page 9: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

Discovery: Catalogs & Containers

var deploymentCatalog = new DeploymentCatalog();

var container = new CompositionContainer(deploymentCatalog);

container.ComposeParts(this);

Wraps parts in the current XAP

Using the parts in the container, find allparts in the current class, then satisfyall imports with corresponding exports

(“Composition”)

Look Ma! No

Bootstrapper!

Page 10: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

DemoHello, MEF!

Page 11: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Notice that composition happens “explicitly” with the composition call

• What about XAML objects? These are created by the XAML parser, not MEF

• How do you share a container across the application?

• CompositionInitializer solves this problem by creating a default, global container

• Must be high level: classes that use this cannot export themselves!

CompositionInitializer

Page 12: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Guarantees imports are satisfied• Works throughout the entire hierarchy• Helps keep plug-ins “honest”• What about multiple plug-ins?

Stable Composition

Page 13: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Allows multiple implementations• Stable composition will reject invalid

exports

ImportMany

Page 14: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

DemoImportMany

Page 15: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• What’s wrong with Singletons?– Change the behavior of the class simply to

modify the lifetime of the class (poor separation of concerns)

– Tough to test and mock– Often abused and create dependencies

• PartCreationPolicy• RequiredCreationPolicy• Factory

Lifetime Management

Page 16: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

DemoMVVM & Lifetime Management

Page 17: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Use composition for the run time• Use design prefixes for design-time

– Composition will fail in the designer due to a different runtime for Cider, but this is fine if you follow the pattern described

– Use mock utilities that can mock an object for

the interface • In test, you can export a different

implementation or simply set mocks directly

Bonus: Design and Test with MEF

Page 18: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Most inversion of control/dependency injection frameworks help wire what you know (bootstrapper)

• MEF provides for what you don’t know• For non-extensible applications, this

still allows for modularity• For extensible applications, this allows

for plug-ins

Extensibility

Page 19: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• First, use an AggregateCatalog to allow multiple XAP files

• The DeploymentCatalog allows asynchronous downloads for XAP files

• Use a new Silverlight Application to create satellite assemblies

• CompositionHost will override the default container• CopyLocal = false for duplicate references• AllowRecomposition• ObservableCollection

Extensibility: Dynamic XAP Files

Page 20: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

DemoDynamic XAP File

Page 21: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

• Add additional information about an export

• Filter exports without invoking/creating them

• Allows for plug-ins to only be generated in context

• Strongly typed• Uses the pattern Lazy<T,TMetadata>

Metadata

Page 22: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

DemoMetadata with Jounce

Page 23: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.comconsulting training design debugging

1. It’s out of the box2. It provides Inversion of Control/Discovery3. It gives you lifetime management4. It can easily manage application wide configuration5. It provides factories that resolve their own dependencies6. It allows for multiple implementations of the same contract

(pipeline and chain of responsibility)7. It filters with strongly-typed metadata8. It dynamically loads modules and extends existing

applications9. It provides for modularity and clean separation of concerns10. It is completely customizable to address your specific needs

(i.e. ExportFactory)

10 Practical Reasons to Use MEF

Page 24: Introduction to the Managed Extensibility Framework in Silverlight

wintellect.com

Questions?

consulting training design debugging

Jeremy LiknessProject Manager, Senior [email protected]