Introduction to the Managed Extensibility Framework in Silverlight
-
Upload
jeremy-likness -
Category
Technology
-
view
970 -
download
2
description
Transcript of 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
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
wintellect.comconsulting training design debugging
• Why MEF?• Discovery• Lifetime Management• Extensibility• Metadata• 10 Practical Reasons to use
MEF
Agenda
wintellect.comconsulting training design debugging
• Misconception: Managed Extensibility Framework
• Part of the framework• Discovery• Lifetime management• Extensibility• Metadata
Why MEF?
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!
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!
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
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
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!
wintellect.comconsulting training design debugging
DemoHello, MEF!
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
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
wintellect.comconsulting training design debugging
• Allows multiple implementations• Stable composition will reject invalid
exports
ImportMany
wintellect.comconsulting training design debugging
DemoImportMany
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
wintellect.comconsulting training design debugging
DemoMVVM & Lifetime Management
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
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
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
wintellect.comconsulting training design debugging
DemoDynamic XAP File
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
wintellect.comconsulting training design debugging
DemoMetadata with Jounce
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
wintellect.com
Questions?
consulting training design debugging
Jeremy LiknessProject Manager, Senior [email protected]