Your SlideShare is downloading. ×
MEF Deep Dive by Piotr Wlodek
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MEF Deep Dive by Piotr Wlodek

2,053
views

Published on

Published in: Technology

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,053
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
46
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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.
  • Transcript

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

    ×