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

PPTX
Wintellect - Devscovery - Portable Class Library
PDF
Automated Visual Regression Testing by Dave Sadlon
PDF
Behaviour testing for single-page applications and API’s
PDF
Containerizing legacy applications
PPTX
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
PDF
Introduction to PHP H/MVC Frameworks by www.silicongulf.com
PPTX
Modular enablement
PDF
Drupal Continuous Integration and devops - Beyond Jenkins
Wintellect - Devscovery - Portable Class Library
Automated Visual Regression Testing by Dave Sadlon
Behaviour testing for single-page applications and API’s
Containerizing legacy applications
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
Introduction to PHP H/MVC Frameworks by www.silicongulf.com
Modular enablement
Drupal Continuous Integration and devops - Beyond Jenkins

What's hot (19)

PDF
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
PDF
Building an Eclipse plugin to recommend changes to developers
PDF
Java Framework comparison
PDF
Laird Best Practices Ajax World West2008
PPTX
Dependency injection via annotations v1.0
PPTX
01 introduction to entity framework
PDF
Natural Language Classifier - Handbook (IBM)
PDF
30 Skills to Master to Become a Senior Software Engineer
PDF
Drupal 8 Involvement with Promet Source
PPTX
Most Useful Design Patterns
PPTX
Improving the Design of Existing Software
PPTX
Basic Selenium Training
PDF
Microsoft power point automation-opensourcetestingtools_matrix-1
KEY
Get your Project back in Shape!
PDF
jp06_bossola
PPTX
05 managing transactions
PPT
slide to delete
PPT
software technology benchmarking
PDF
[English version] JavaFX and Web Integration
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
Building an Eclipse plugin to recommend changes to developers
Java Framework comparison
Laird Best Practices Ajax World West2008
Dependency injection via annotations v1.0
01 introduction to entity framework
Natural Language Classifier - Handbook (IBM)
30 Skills to Master to Become a Senior Software Engineer
Drupal 8 Involvement with Promet Source
Most Useful Design Patterns
Improving the Design of Existing Software
Basic Selenium Training
Microsoft power point automation-opensourcetestingtools_matrix-1
Get your Project back in Shape!
jp06_bossola
05 managing transactions
slide to delete
software technology benchmarking
[English version] JavaFX and Web Integration
Ad

Similar to Introduction to the Managed Extensibility Framework in Silverlight (20)

PPTX
Wintellect - Windows 8 for the Silverlight and WPF Developer
PPTX
Silverlight 5
PPTX
Managed Extensibility Framework or Effective Development of Scalable Applicat...
PDF
Managed Extensibility Framework
PPTX
My XML is Alive! An Intro to XAML
PPT
Extending the Enterprise with MEF
PDF
Smart Client Development
PPTX
Nashua Cloud .NET User Group - Basic WP8 App Dev With XAML and C#, April 2013
PPTX
How to Build Composite Applications with PRISM
PDF
10 Ways To Improve Your Code( Neal Ford)
PPTX
Microsoft Stack Visual Studio 2010 Overview
PDF
Mike Taulty DevDays 2010 Silverlight MEF
PPT
Mef 12 step program
PPTX
Visual State Manager
PPTX
MVVM for Modern Applications
PPTX
Rethinking Object Orientation
PDF
Developer’s guide to microsoft unity
PPTX
Building extensible application using MEF
PDF
Windows App Development Online Training.pdf
PPTX
Diagnosing MEF Failure
Wintellect - Windows 8 for the Silverlight and WPF Developer
Silverlight 5
Managed Extensibility Framework or Effective Development of Scalable Applicat...
Managed Extensibility Framework
My XML is Alive! An Intro to XAML
Extending the Enterprise with MEF
Smart Client Development
Nashua Cloud .NET User Group - Basic WP8 App Dev With XAML and C#, April 2013
How to Build Composite Applications with PRISM
10 Ways To Improve Your Code( Neal Ford)
Microsoft Stack Visual Studio 2010 Overview
Mike Taulty DevDays 2010 Silverlight MEF
Mef 12 step program
Visual State Manager
MVVM for Modern Applications
Rethinking Object Orientation
Developer’s guide to microsoft unity
Building extensible application using MEF
Windows App Development Online Training.pdf
Diagnosing MEF Failure
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Building Integrated photovoltaic BIPV_UPV.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

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

  • #4: 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?”
  • #7: 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.
  • #14: Copy the Hello MEF. Convert to a ListBox. Change Text to an array and then set the items source.
  • #16: 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.
  • #17: 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.
  • #21: 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.
  • #23: Jounce is a framework that provides specific guidance for MVVM with MEF.
  • #24: If time permits, go back to some of the case studies and discuss how they worked with MEF.