Successfully reported this slideshow.
Managed Extensibility Framework<br />Deep Dive<br />Piotr Włodek<br />
Agenda<br />Managed Extensibility Framework<br />Extending MEF<br />
Breaks up large chunks of functionality into smaller pieces<br />Allows the teams to work in parallel<br />Allows to deliv...
Design pattern<br />Creates graphs of objects<br />Manages dependencies between objects<br />Manages lifetime of the objec...
IoC + MAF = MEF ?<br />Managed Addin Framework<br />IoC Container<br />Managed Extensibility Framework<br />
An extensible framework for composing applications from a set of loosely-coupled componentsdiscovered and evolving at runt...
Composable parts.<br />Application consists of composable parts<br />
Parts. Exports. Imports.<br />import<br />import<br />import<br />Part<br />export<br />export<br />
PartDefinition is a blueprint for a Part(similar to .NET Type and Object)<br />Part represents the exported value<br />Par...
Exportit.<br />[Export(typeof(ILogger))]<br />public class Logger : ILogger<br />{<br />    public void Info(string messag...
Importit.<br />public class Program<br />{<br />[Import]<br />public ILogger Logger { get; set; }<br />private static void...
Container takes parts from catalogs.<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />...
Composeit.<br />import<br />import<br />import<br />import<br />import<br />import<br />import<br />part<br />part<br />pa...
Composeit.<br />Catalogsprovide composableparts.<br />Compose<br />Catalog<br />
Composeit.<br />Container wires up composable parts together.<br />Compose<br />Catalog<br />
Compose it.<br />private void Run()<br />{<br />    var catalog = new AssemblyCatalog(...);<br />    var container = <br /...
Demo 1 – MEF basics<br />
AvailableCatalogs.<br />AggregateCatalog<br />DirectoryCatalog<br />DeploymentCatalog<br />AssemblyCatalog<br />TypeCatalo...
Types<br />Properties<br />Fields<br />Methods<br />Exportable components.<br />
Exportit.<br />public class Utils<br />{<br />[Export("Logger.Prompt")]<br />    public string LoggerPrompt {<br />       ...
Importit.<br />[Export(typeof(ILogger))]<br />public class FunnyLogger : ILogger<br />{<br />    [Import("Logger.Prompt")]...
DEMO 2 – EXPORTS<br />
Which parts goes together ?<br />import<br />import<br />import<br />part<br />export<br />export<br />?<br />import<br />...
Demo 3 - WidgetS<br />
Metadata.<br />Where to place that widget ?<br />Widget<br />Widget<br />
Exportit. Metadata.<br />[Export(typeof(IWidget))]<br />[ExportMetadata("Location", WidgetLocation.Right)]<br />public par...
Importit. Metadata.<br />[Export]<br />public class ShellPresentationModel<br />{<br />[ImportMany]<br />    public Lazy<I...
Demo 4 – Widgets and metadata<br />
Exportit. In a customway.<br />[Export(typeof(IWidget))]<br />[ExportMetadata("Location", WidgetLocation.Right)]<br />publ...
Exportit. In a customway.<br />[Widget(WidgetLocation.Right)]<br />public partial class TwitterWidget : ITwitterWidget<br ...
Demo 5 – custom export<br />
Recomposition.<br />Parts introduced to or removed from the container may have an impact on this import – a part can opt-i...
Recomposition.<br />[Export]<br />public class ShellPresentationModel<br />{<br />[ImportMany(AllowRecomposition = true)]<...
DEMO 5 - recomposition<br />
Stable composition.<br />Parts with missing required imports are rejected.<br />part<br />requires zero or more<br />requi...
Demo 6 – Stable composition<br />
Visual Studio Output Window<br />Microsoft.ComponentModel.Composition.Diagnostics.dll<br />Mefx<br />mefx /file:MyAddIn.dl...
Visual MEFX<br />
Demo 7 – debugging mef<br />
Trim up your apps, break up your xaps!<br />Available only in Silverlight<br />DeploymentCatalog<br />
Demo 8 – deployment catalog<br />
A set of extensions developed by community<br />Where can I find it ?<br />www.mefcontrib.com<br />www.mefcontrib.codeplex...
InterceptingCatalog– enables interception<br />ConventionCatalog– conventions based programming model<br />FilteringCatalo...
Demo 9 – mef contrib<br />
Extending MEF<br />MEF<br />Programming Models<br />Export Providers<br />
Export Providers.<br />CompositionContainer<br />CatalogExportProvider<br />ExportProvider<br />ExportProvider<br />Catalo...
MEF ships with Attributed programming model<br />Programming models are extensible<br />Programming models.<br />MEF Primi...
Upcoming SlideShare
Loading in …5
×

MEF Deep Dive by Piotr Wlodek

2,859 views

Published on

Published in: Technology
  • Be the first to comment

MEF Deep Dive by Piotr Wlodek

  1. 1. Managed Extensibility Framework<br />Deep Dive<br />Piotr Włodek<br />
  2. 2. Agenda<br />Managed Extensibility Framework<br />Extending MEF<br />
  3. 3. Breaks up large chunks of functionality into smaller pieces<br />Allows the teams to work in parallel<br />Allows to deliver additional functionality by 3rd parties<br />Isolation – if a plugin crashes, the rest of the app keeps working<br />Plug-ins Modules<br />Managed Addin Framework<br />
  4. 4. Design pattern<br />Creates graphs of objects<br />Manages dependencies between objects<br />Manages lifetime of the objects<br />Dependency Injection<br />P&P Unity 2.0<br />public SampleViewModel(<br /> IShell shell,<br /> ISampleView view,<br /> IEventAggregator eventAggregator,<br /> IPersistenceFacade persistenceManager,<br /> ILoggerService loggerService,<br /> IExceptionManager exceptionManager,<br /> IWindowManagerExt windowManager) { ... }<br />Autofac<br />Ninject<br />Castle<br />
  5. 5. IoC + MAF = MEF ?<br />Managed Addin Framework<br />IoC Container<br />Managed Extensibility Framework<br />
  6. 6. An extensible framework for composing applications from a set of loosely-coupled componentsdiscovered and evolving at runtime <br />Applications are no longer statically compiled, they are dynamically composed(Recomposition)<br />Supported in BCL 4.0 and Silverlight 4 <br />Available for .NET 3.5 / SL 3 (Codeplex Drop 9)<br />MEF 2 Drop 2 available for download!<br />Lives in the System.ComponentModel.Composition.dll<br />SL has also some additional stuff in the System.ComponentModel.Composition.Initialization.dll<br />Managed Extensibility Framework<br />
  7. 7. Composable parts.<br />Application consists of composable parts<br />
  8. 8. Parts. Exports. Imports.<br />import<br />import<br />import<br />Part<br />export<br />export<br />
  9. 9. PartDefinition is a blueprint for a Part(similar to .NET Type and Object)<br />Part represents the exported value<br />Parts and their definitions.<br />Object<br />GetExportedValue()<br />import<br />import<br />import<br />Part<br />export<br />export<br />import<br />import<br />CreatePart()<br />PartDefinition<br />PartDefinition<br />export<br />export<br />
  10. 10. Exportit.<br />[Export(typeof(ILogger))]<br />public class Logger : ILogger<br />{<br /> public void Info(string message)<br /> {<br /> Console.WriteLine(message);<br /> }<br />}<br />Logger<br />ILogger<br />Export<br />
  11. 11. Importit.<br />public class Program<br />{<br />[Import]<br />public ILogger Logger { get; set; }<br />private static void Main() {<br /> var p = new Program();<br /> p.Run();<br />}<br />}<br />Program<br />ILogger<br />Import<br />
  12. 12. Container takes parts from catalogs.<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />Catalog<br />Catalog<br />Catalog<br />Catalog<br />CompositionContainer<br />
  13. 13. Composeit.<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 />export<br />Composition Container<br />part<br />part<br />part<br />part<br />part<br />part<br />
  14. 14. Composeit.<br />Catalogsprovide composableparts.<br />Compose<br />Catalog<br />
  15. 15. Composeit.<br />Container wires up composable parts together.<br />Compose<br />Catalog<br />
  16. 16. Compose it.<br />private void Run()<br />{<br /> var catalog = new AssemblyCatalog(...);<br /> var container = <br /> new CompositionContainer(catalog);<br /> container.ComposeParts(this);<br /> Logger.Info("Hello World!");<br />}<br />Program<br />Compose<br />
  17. 17. Demo 1 – MEF basics<br />
  18. 18. AvailableCatalogs.<br />AggregateCatalog<br />DirectoryCatalog<br />DeploymentCatalog<br />AssemblyCatalog<br />TypeCatalog<br />
  19. 19. Types<br />Properties<br />Fields<br />Methods<br />Exportable components.<br />
  20. 20. Exportit.<br />public class Utils<br />{<br />[Export("Logger.Prompt")]<br /> public string LoggerPrompt {<br /> get { return "Logger Prompt: "; }<br /> }<br />[Export("Logger.ProcessText")]<br /> public string Process(string input) {<br /> ...<br /> }<br />}<br />Utils<br />"Logger.Prompt„<br />"Logger.ProcessText"<br />Export<br />
  21. 21. Importit.<br />[Export(typeof(ILogger))]<br />public class FunnyLogger : ILogger<br />{<br /> [Import("Logger.Prompt")]<br /> private string m_Prompt;<br />[Import("Logger.ProcessText")]<br /> private Func<string, string> m_TextFun;<br />}<br />Funny<br />Logger<br />"Logger.Prompt"<br />"Logger.ProcessText"<br />Import<br />
  22. 22. DEMO 2 – EXPORTS<br />
  23. 23. Which parts goes together ?<br />import<br />import<br />import<br />part<br />export<br />export<br />?<br />import<br />part<br />
  24. 24. Demo 3 - WidgetS<br />
  25. 25. Metadata.<br />Where to place that widget ?<br />Widget<br />Widget<br />
  26. 26. Exportit. Metadata.<br />[Export(typeof(IWidget))]<br />[ExportMetadata("Location", WidgetLocation.Right)]<br />public partial class TwitterWidget : ITwitterWidget<br />{<br />[Import]<br /> public TwitterWidgetPresentationModel PresentationModel<br /> {<br /> get;<br /> set;<br /> }<br />}<br />Twitter<br />Widget<br />Put me on the right<br />Export<br />IWidget<br />
  27. 27. Importit. Metadata.<br />[Export]<br />public class ShellPresentationModel<br />{<br />[ImportMany]<br /> public Lazy<IWidget, IWidgetMetadata>[] Widgets <br /> { get; private set; }<br />}<br />ShellPM<br />Collection of Lazy IWidgets with IWidgetMetadata <br />Import<br />
  28. 28. Demo 4 – Widgets and metadata<br />
  29. 29. Exportit. In a customway.<br />[Export(typeof(IWidget))]<br />[ExportMetadata("Location", WidgetLocation.Right)]<br />public partial class TwitterWidget : ITwitterWidget<br />{<br />[Import]<br /> public TwitterWidgetPresentationModel Model<br /> {<br /> get;<br /> set;<br /> }<br />}<br />Twitter<br />Widget<br />Put me on the right<br />IWidget<br />Export<br />
  30. 30. Exportit. In a customway.<br />[Widget(WidgetLocation.Right)]<br />public partial class TwitterWidget : ITwitterWidget<br />{<br />[Import]<br /> public TwitterWidgetPresentationModel Model<br /> {<br /> get;<br /> set;<br /> }<br />}<br />Twitter<br />Widget<br />Put me on the right<br />IWidget<br />Export<br />
  31. 31. Demo 5 – custom export<br />
  32. 32. Recomposition.<br />Parts introduced to or removed from the 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 />part<br />CompositionContainer<br />Catalog<br />Catalog<br />Catalog<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />PartDefinition<br />
  33. 33. Recomposition.<br />[Export]<br />public class ShellPresentationModel<br />{<br />[ImportMany(AllowRecomposition = true)]<br /> public Lazy<IWidget, IWidgetMetadata>[] Widgets<br /> { <br /> get; private set;<br /> }<br />}<br />ShellPM<br />Collection of Lazy IWidgets with IWidgetMetadata <br />Import<br />
  34. 34. DEMO 5 - recomposition<br />
  35. 35. Stable composition.<br />Parts with missing required imports are rejected.<br />part<br />requires zero or more<br />requires<br />part<br />part<br />requires<br />part<br />part<br />requires<br />missing<br />
  36. 36. Demo 6 – Stable composition<br />
  37. 37. Visual Studio Output Window<br />Microsoft.ComponentModel.Composition.Diagnostics.dll<br />Mefx<br />mefx /file:MyAddIn.dll /directory:dir /rejected /verbose<br />Debugging MEF<br />[Part] ClassLibrary1.ChainOne from: AssemblyCatalog (Assembly="ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") [Primary Rejection] <br />[Export] ClassLibrary1.ChainOne (ContractName="ClassLibrary1.ChainOne") <br />[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No valid exports were found that match the constraint '((exportDefinition.ContractName == "ClassLibrary1.ChainTwo") AndAlso exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "ClassLibrary1.ChainTwo".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.<br />
  38. 38. Visual MEFX<br />
  39. 39. Demo 7 – debugging mef<br />
  40. 40. Trim up your apps, break up your xaps!<br />Available only in Silverlight<br />DeploymentCatalog<br />
  41. 41. Demo 8 – deployment catalog<br />
  42. 42. A set of extensions developed by community<br />Where can I find it ?<br />www.mefcontrib.com<br />www.mefcontrib.codeplex.com<br />http://github.com/MefContrib/<br />MefContrib<br />MefContrib-Samples<br />MefContrib-Tools<br />MEF Contrib<br />
  43. 43. InterceptingCatalog– enables interception<br />ConventionCatalog– conventions based programming model<br />FilteringCatalog – enables parts filtering based on any criteria<br />GenericCatalog – enables support for open-generics<br />FactoryExportProvider – factory based registration<br />IoC integration layer – MEF / Unity<br />MefContrib – what’s in it for me?<br />
  44. 44. Demo 9 – mef contrib<br />
  45. 45. Extending MEF<br />MEF<br />Programming Models<br />Export Providers<br />
  46. 46. Export Providers.<br />CompositionContainer<br />CatalogExportProvider<br />ExportProvider<br />ExportProvider<br />Catalog<br />AggregateExportProvider<br />CatalogExportProvider<br />ExportProvider<br />ExportProvider<br />CompositionContainer<br />ExportProvider<br />
  47. 47. MEF ships with Attributed programming model<br />Programming models are extensible<br />Programming models.<br />MEF Primitives<br />ReflectionModelServices<br />ExportDef<br />PartDef<br />ImportDef<br />
  48. 48. + Ease of programming<br />+ Resolves dependencies between components <br />+ Automatic component discovery<br />+ Can compose types, fields, props and methods<br />- Slower than the IoC containers<br />- Lack of some IoC features<br />Method injection<br />Assisted injection<br />Lifetime management<br />- No component separation (separate appdomain, process)<br />MEF vs IoC vs MAF<br />
  49. 49. Managed Extensibility Framework<br />http://mef.codeplex.com/<br />MEF in MSDN Magazine<br />http://msdn.microsoft.com/en-us/magazine/ee291628.aspx<br />MEF Contrib<br />http://mefcontrib.com/<br />Glenn Block’s Blog<br />http://blogs.msdn.com/b/gblock/<br />Piotr Włodek’s Blog<br />http://pwlodek.blogspot.com/<br />Useful links<br />

×