SlideShare a Scribd company logo
Intro to MEF with Silverlight




                            Jeremy Likness
                            Project Manager, Senior Consultant
                            jlikness@wintellect.com
                                                                   Copyright © 2011




consulting   training   design   debugging                       wintellect.com
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 Production


consulting    training    design     debugging                                   wintellect.com
Agenda

 • Why MEF?
 • Discovery
 • Lifetime Management
 • Extensibility
 • Metadata
 • 10 Practical Reasons to use MEF
consulting   training   design   debugging   wintellect.com
Why MEF?

 • Misconception: Managed Extensibility
   Framework
 • Part of the framework
 • Discovery
 • Lifetime management
 • Extensibility
 • Metadata


consulting   training   design   debugging   wintellect.com
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)
 • SharePoint


consulting   training   design   debugging   wintellect.com
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                            wintellect.com
Discovery: Catalogs

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



consulting   training   design   debugging   wintellect.com
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 containers
consulting   training   design   debugging   wintellect.com
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                               wintellect.com
Demo
   Hello, MEF!




consulting   training   design   debugging   wintellect.com
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   wintellect.com
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   wintellect.com
ImportMany

 • Allows multiple implementations
 • Stable composition will reject invalid
   exports




consulting   training   design   debugging   wintellect.com
Demo
   ImportMany




consulting   training   design   debugging   wintellect.com
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
 • Factory


consulting   training   design   debugging            wintellect.com
Demo
   MVVM & Lifetime Management




consulting   training   design   debugging   wintellect.com
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 directly

consulting   training   design   debugging              wintellect.com
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-ins

consulting   training   design   debugging   wintellect.com
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
 • ObservableCollection



consulting   training   design   debugging           wintellect.com
Demo
   Dynamic XAP File




consulting   training   design   debugging   wintellect.com
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   wintellect.com
Demo
   Metadata with Jounce




consulting   training   design   debugging   wintellect.com
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                      wintellect.com
Questions?




                            Jeremy Likness
                            Project Manager, Senior Consultant
                            jlikness@wintellect.com



consulting   training   design   debugging                       wintellect.com

More Related Content

Introduction to the Managed Extensibility Framework in Silverlight

  • 1. Intro to MEF with Silverlight Jeremy Likness Project Manager, Senior Consultant jlikness@wintellect.com Copyright © 2011 consulting training design debugging wintellect.com
  • 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 Production consulting training design debugging wintellect.com
  • 3. Agenda • Why MEF? • Discovery • Lifetime Management • Extensibility • Metadata • 10 Practical Reasons to use MEF consulting training design debugging wintellect.com
  • 4. Why MEF? • Misconception: Managed Extensibility Framework • Part of the framework • Discovery • Lifetime management • Extensibility • Metadata consulting training design debugging wintellect.com
  • 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) • SharePoint consulting training design debugging wintellect.com
  • 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 wintellect.com
  • 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 Catalogs consulting training design debugging wintellect.com
  • 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 containers consulting training design debugging wintellect.com
  • 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 wintellect.com
  • 10. Demo Hello, MEF! consulting training design debugging wintellect.com
  • 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 wintellect.com
  • 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 wintellect.com
  • 13. ImportMany • Allows multiple implementations • Stable composition will reject invalid exports consulting training design debugging wintellect.com
  • 14. Demo ImportMany consulting training design debugging wintellect.com
  • 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 • Factory consulting training design debugging wintellect.com
  • 16. Demo MVVM & Lifetime Management consulting training design debugging wintellect.com
  • 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 directly consulting training design debugging wintellect.com
  • 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-ins consulting training design debugging wintellect.com
  • 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 • ObservableCollection consulting training design debugging wintellect.com
  • 20. Demo Dynamic XAP File consulting training design debugging wintellect.com
  • 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 wintellect.com
  • 22. Demo Metadata with Jounce consulting training design debugging wintellect.com
  • 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 wintellect.com
  • 24. Questions? Jeremy Likness Project Manager, Senior Consultant jlikness@wintellect.com consulting training design debugging wintellect.com

Editor's Notes

  1. I’ve been talking about MEF for awhile. At many of my talks, I’m stopped with “Jeremy, hold on, you’re showing us advanced uses of MEF. But why do I even want to use MEF in the first? What is it exactly that MEF does?”
  2. Discovery is what most people are looking to do when they implement Inversion of Control and Dependency Injection. There are many ways to do discovery from factories and service locators to bootstrappers and IoC containers. MEF provides a very simple and straightforward way to handle discovery.
  3. Copy the Hello MEF. Convert to a ListBox. Change Text to an array and then set the items source.
  4. Creation policies are run always once, when the imports are satisfied. Factory is unique in that it provides a means for creating new instances, and works independent of the part creation policy.
  5. This demo is a full-fledged MEF/MVVM solution. It shows not only the lifetime management concept, but how MEF works with MVVM to drive Silverlight applications. It also shows design-time support with MEF.
  6. Copy the import many. Add an aggregate catalog and use that in the container and introduce CompositionHost. Disable the button on click. Change the text to an observable collection, then add the new project with the export. Show that the button click does NOT actually pull the new export. Then add AllowRecomposition and show it working.
  7. Jounce is a framework that provides specific guidance for MVVM with MEF.
  8. If time permits, go back to some of the case studies and discuss how they worked with MEF.