MEF Deep Dive by Piotr Wlodek

2,673 views

Published on

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

No Downloads
Views
Total views
2,673
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
57
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • We are gonna talks about MEF, and how to write EXTENSIBLE applications!
  • DI is a design pattern in witch a special entity, usualy a container, is responsible for creating and managing the graph object.If you were to create the instance yourself you would have to inject all dependencies yourself.
  • MEF combines the best features of the IoC frameworks with MAF
  • Composable part is MEF’s representation of a class instance.
  • MEF out of the box ships with the Attributed Programming Model.
  • MEF composes available parts together.
  • MEF doesn’t allow to create parts in inconsistent state.
  • 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 />

    ×