MEF in Silverlight 4 - a guided tour

1,356 views

Published on

Mike Taulty from Microsoft gives a detailed exploration of the Microsoft Extensibility Framework (MEF) with Silverlight 4 to the Silverlight UK User Group.

This presentation was given on 3rd February2010 at the Microsoft "Cardinal Place" office in London.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,356
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MEF in Silverlight 4 - a guided tour

  1. 1. MEF in Silverlight 4 – a guided tour<br />Mike Taulty<br />Microsoft Ltd<br />mtaulty@microsoft.com<br />mtaulty.com<br />twitter.com/mtaulty<br />
  2. 2. [background]<br />
  3. 3. MEF – What is it? How do I get it?<br />the Managed Extensibility Framework<br />an extensible framework for composing applications from a set of loosely-coupled parts discovered and evolving at run-time <br />versions<br />.NET Framework shipping with Silverlight 4 <br />.NET Framework 4 shipping with VS 2010<br />Silverlight 3 & .NET Framework V3.5 Sp1<br />Source on CodePlex<br />
  4. 4. “scenario”<br />iTunesMediaPlayer<br />Constructor()<br />GetAlbumInfo(“Blood on the Tracks”)<br />IAlbumInfoProvider<br />AlbumInfoProvider<br />Constructor()<br />GetAlbumArt(“Blood on the Tracks”)<br />IAlbumArtProvider<br />AlbumArtProvider<br />
  5. 5. “scenario”<br />iTunesMediaPlayer<br />Constructor()<br />GetAlbumInfo(“Blood on the Tracks”)<br />AlbumInfoProvider<br />Constructor()<br />GetAlbumArt(“Blood on the Tracks”)<br />AlbumArtProvider<br /><ul><li>Static dependency AlbumInfoProvider->AlbumArtProvider
  6. 6. Hard to test AlbumInfoProvider in isolation from AlbumArtProvider
  7. 7. Hard to drop in a different AlbumArtProvider implementation
  8. 8. Code changes to AlbumArtProvider impact AlbumInfoProvider</li></li></ul><li>Inversion of Control ( IoC )<br />iTunesMediaPlayer<br />IAlbumArtProvider<br />Constructor()<br />Works ok for the single dependency case but this won’t scale to trees of objects of arbitrary complexity<br />AlbumArtProvider<br />AlbumArtProvider<br />Constructor(IAlbumArtProvider)<br />GetAlbumInfo(“Blood on the Tracks”)<br />AlbumInfoProvider<br />GetAlbumArt(“Blood on the Tracks”)<br /><ul><li>Static dependency AlbumInfoProvider->IAlbumArtProvider
  9. 9. Easier to test AlbumInfoProvider with anyIAlbumArtProvider
  10. 10. Easier to drop in new implementations of IAlbumArtProvider
  11. 11. Version changes to AlbumArtProvider do not impact AlbumInfoProvider</li></li></ul><li>the IoC Container<br />“RegisterMapping “<br />typeof(IAlbumArtProvider)  typeof(AlbumArtProvider)<br />“RegisterMapping “<br />typeof(IAlbumInfoProvider)  typeof(AlbumInfoProvider)<br />Container<br />
  12. 12. the IoC Container<br />Container<br />iTunesMediaPlayer<br />Get&lt;IAlbumInfoProvider&gt;()<br />AlbumArtProvider<br />AlbumArtProvider<br />Constructor(IAlbumArtProvider)<br />Constructor(IAlbumArtProvider)<br />AlbumInfoProvider<br />AlbumInfoProvider<br />
  13. 13. [end of background]<br />
  14. 14. example ( Unity IoC )<br />
  15. 15. but MEF is not ‘just’ another IoC container<br />an extensible framework for composing applications from a set of loosely-coupled parts discovered and evolving at run-time <br />
  16. 16. using MEF in our scenario<br />demo<br />
  17. 17. MEF models composable parts<br />import<br />import<br />import<br />Part<br />export<br />export<br />
  18. 18. MEF composes parts<br />import<br />import<br />import<br />import<br />import<br />import<br />import<br />part<br />part<br />part<br />part<br />import<br />import<br />export<br />export<br />export<br />Composition Engine<br />export<br />part<br />part<br />part<br />part<br />part<br />
  19. 19. which parts compose together?<br />import<br />import<br />import<br />part<br />export<br />export<br />?<br />import<br />part<br />
  20. 20. matching imports to exports<br />demo<br />
  21. 21. parts & part definitions<br />import<br />import<br />import<br />import<br />Part<br />PartDefinition<br />CreatePart()<br />export<br />export<br />export<br />PartDefinition is a blueprint for a Part<br />similar to Object and Type in .NET<br />but can have Part without PartDefinition<br />
  22. 22. container draws parts from catalogs<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />import<br />import<br />import<br />PartDefinition<br />import<br />import<br />import<br />PartDefinition<br />import<br />import<br />import<br />PartDefinition<br />Part<br />Part<br />Part<br />Catalog<br />Catalog<br />Catalog<br />export<br />export<br />export<br />export<br />export<br />export<br />Catalog<br />CompositionContainer<br />
  23. 23. or mix in pre-instantiated parts<br />part<br />Imports(“Foo”)<br />Compose()<br />part<br />Exports(“Foo”)<br />CompositionContainer<br />
  24. 24. the built-in catalogs<br />demo<br />
  25. 25. things change - recomposition<br />New parts [introduced to/removed from] container may have an impact on this import – a part can opt-in to allow this recomposition<br />part<br />Compose()<br />part?<br />part<br />CompositionContainer<br />part<br />Catalog<br />Catalog<br />Catalog<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />Some catalogs support additions<br />
  26. 26. recomposition is key for Silverlight<br />initial, small download<br />MyApp.xap<br />Extras.xap<br />additional functionality <br />
  27. 27. recomposition & <br />the DeploymentCatalog<br />demo<br />
  28. 28. stable composition<br />reject parts with unsatisfied imports<br />reject changes that break existing “agreements”<br />part<br />part<br />requires exactly<br />one part<br />requires<br />part<br />part<br />requires<br />CompositionContainer<br />missing<br />different part,<br />same contract<br />part<br />
  29. 29. stable composition<br />demo<br />
  30. 30. MEF and Model View ViewModel<br />Strong separation of concerns possible with Silverlight development<br />user interface declared in XAML<br />loosely binds to data<br />functionality invoked by declarative commands<br />Encapsulated by the “MVVM” pattern<br />binding<br />model<br />(“data”)<br />viewmodel<br />(state & data for view)<br />view<br />(declarative XAML)<br />commands<br />
  31. 31. MVVM via MEF<br />demo<br />
  32. 32. resources<br />Glenn Block<br />blogs.msdn.com/gblock<br />Nicholas Blumhardt<br />blogs.msdn.com/nblumhardt<br />MEF on CodePlex<br />mef.codeplex.com<br />Channel 9 Silverlight 4 MEF Screencasts<br />channel9.msdn.com/tags/learnMEFSL4<br />
  33. 33. feedbackMicrosoft Session Feedback via Twitter<br />@msftsession 10 @mtaulty #sluguk ...<br />session score out of 10<br />( 10 is recommended but other scores are possible )<br />

×