Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to the Managed Extensibility Framework in Silverlight


Published on

Learn how to use the Managed Extensibility Framework in Silverlight.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Introduction to the Managed Extensibility Framework in Silverlight

  1. 1. Intro to MEF with Silverlight Jeremy Likness Project Manager, Senior Consultant Copyright © 2011consulting training design debugging
  2. 2. what we do consulting training design debugging who we are 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. how we do it Training • On-site instructor-led training Consulting & Debugging • Virtual instructor-led training • Architecture, analysis, and design services • Devscovery conferences • Full lifecycle custom software development • Content creation Design • Project management • User Experience Design • Debugging & performance tuning • Visual & Content Design • Video & Animation Productionconsulting training design debugging
  3. 3. Agenda • Why MEF? • Discovery • Lifetime Management • Extensibility • Metadata • 10 Practical Reasons to use MEFconsulting training design debugging
  4. 4. Why MEF? • Misconception: Managed Extensibility Framework • Part of the framework • Discovery • Lifetime management • Extensibility • Metadataconsulting training design debugging
  5. 5. Welcome to the MEF Lab! • 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) • SharePointconsulting training design debugging
  6. 6. Discovery • Import: Contract and Demand • Export: Implementation and Supply Look Ma! I • Part: Supply & Demand need 500 don’t constructor overloads! “I need something that implements IMessage” [Import] public IMessage MyMessenger { get; set; } [Export(typeof(IMessage))] public class SpecificMessenger : IMessage “I have something that implements IMessage”consulting training design debugging
  7. 7. Discovery: Catalogs • Catalogs tell MEF where to find parts • AssemblyCatalog: Current assembly • TypeCatalog: List of types • DeploymentCatalog: Silverlight XAP file • AggregateCatalog: Composed of Multiple Catalogsconsulting training design debugging
  8. 8. Discovery: Container • 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 containersconsulting training design debugging
  9. 9. Discovery: Catalogs & Containers Wraps parts in the current XAP Look Ma! No var deploymentCatalog = new DeploymentCatalog(); var container = new Bootstrapper! CompositionContainer(deploymentCatalog); container.ComposeParts(this); Using the parts in the container, find all parts in the current class, then satisfy all imports with corresponding exports (“Composition”)consulting training design debugging
  10. 10. Demo Hello, MEF!consulting training design debugging
  11. 11. CompositionInitializer • 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!consulting training design debugging
  12. 12. Stable Composition • Guarantees imports are satisfied • Works throughout the entire hierarchy • Helps keep plug-ins “honest” • What about multiple plug-ins?consulting training design debugging
  13. 13. ImportMany • Allows multiple implementations • Stable composition will reject invalid exportsconsulting training design debugging
  14. 14. Demo ImportManyconsulting training design debugging
  15. 15. Lifetime Management • 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 • Factoryconsulting training design debugging
  16. 16. Demo MVVM & Lifetime Managementconsulting training design debugging
  17. 17. Bonus: Design and Test with MEF • 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 directlyconsulting training design debugging
  18. 18. Extensibility • 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-insconsulting training design debugging
  19. 19. Extensibility: Dynamic XAP Files • 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 • ObservableCollectionconsulting training design debugging
  20. 20. Demo Dynamic XAP Fileconsulting training design debugging
  21. 21. Metadata • 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>consulting training design debugging
  22. 22. Demo Metadata with Jounceconsulting training design debugging
  23. 23. 10 Practical Reasons to Use MEF 1. It’s out of the box 2. It provides Inversion of Control/Discovery 3. It gives you lifetime management 4. It can easily manage application wide configuration 5. It provides factories that resolve their own dependencies 6. It allows for multiple implementations of the same contract (pipeline and chain of responsibility) 7. It filters with strongly-typed metadata 8. It dynamically loads modules and extends existing applications 9. It provides for modularity and clean separation of concerns 10. It is completely customizable to address your specific needs (i.e. ExportFactory)consulting training design debugging
  24. 24. Questions? Jeremy Likness Project Manager, Senior Consultant jlikness@wintellect.comconsulting training design debugging