mef deep dive by piotr wlodek
TRANSCRIPT
![Page 1: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/1.jpg)
Managed Extensibility Framework
Piotr Włodek
Deep Dive
![Page 2: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/2.jpg)
Agenda
Managed Extensibility Framework
Extending MEF
![Page 3: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/3.jpg)
• 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
![Page 4: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/4.jpg)
• Design pattern• Creates graphs of objects• Manages dependencies between objects• Manages lifetime of the objects
Dependency Injection
public SampleViewModel( IShell shell, ISampleView view, IEventAggregator eventAggregator, IPersistenceFacade persistenceManager, ILoggerService loggerService, IExceptionManager exceptionManager, IWindowManagerExt windowManager) { ... }
P&P Unity 2.0
Autofac Ninject Castle
![Page 5: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/5.jpg)
IoC + MAF = MEF ?
IoC Container
Managed Addin Framework
Managed Extensibility Framework
![Page 6: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/6.jpg)
• An extensible framework for composing applications from a set of loosely-coupled components discovered 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
![Page 7: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/7.jpg)
Composable parts.
• Application consists of composable parts
![Page 8: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/8.jpg)
Parts. Exports. Imports.
Partexportexport
importimportimport
![Page 9: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/9.jpg)
• PartDefinition is a blueprint for a Part (similar to .NET Type and Object)
• Part represents the exported value
Parts and their definitions.
Partexportexport
importimportimport
PartDefinition
export
importPartDefinition
export
import
CreatePart()
ObjectGetExportedValue()
![Page 10: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/10.jpg)
Export it.
Logger
[Export(typeof(ILogger))]public class Logger : ILogger{ public void Info(string message) { Console.WriteLine(message); }}
Export
ILogger
![Page 11: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/11.jpg)
Import it.
Program
public class Program{
[Import]public ILogger Logger { get; set; }
private static void Main() { var p = new Program(); p.Run();}
}
Import
ILogger
![Page 12: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/12.jpg)
Container takes parts from catalogs.
CompositionContainer
Catalog
Catalog Catalog Catalog
PartDefinitionPartDefinitionPartDefinitionPartDefinitionPartDefinitionPartDefinitionPartDefinitionPartDefinition
PartDefinitionPartDefinitionPartDefinitionPartDefinition
![Page 13: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/13.jpg)
Compose it.
part
exportexport
importimport
import
partimportimportimport
partexport
import
part
export
importimport
Composition Container
part
part
part
part
part
part
FE
M
![Page 14: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/14.jpg)
Compose it.
Program
private void Run(){ var catalog = new AssemblyCatalog(...); var container = new CompositionContainer(catalog); container.ComposeParts(this);
Logger.Info("Hello World!");}
Compose
![Page 15: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/15.jpg)
DEMO 1 – MEF BASICS
![Page 16: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/16.jpg)
Available Catalogs.
AssemblyCatalog
TypeCatalog
DirectoryCatalog
AggregateCatalog
DeploymentCatalog
![Page 17: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/17.jpg)
• Types• Properties• Fields• Methods
Exportable components.
![Page 18: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/18.jpg)
Export it.
Utils
public class Utils{ [Export("Logger.Prompt")] public string LoggerPrompt { get { return "Logger Prompt: "; } }
[Export("Logger.ProcessText")] public string Process(string input) { ... }}
Export
"Logger.Prompt„"Logger.ProcessText"
![Page 19: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/19.jpg)
Import it.
FunnyLogger
[Export(typeof(ILogger))]public class FunnyLogger : ILogger{ [Import("Logger.Prompt")] private string m_Prompt;
[Import("Logger.ProcessText")] private Func<string, string> m_TextFun;}
Import
"Logger.Prompt""Logger.ProcessText"
![Page 20: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/20.jpg)
DEMO 2 – EXPORTS
![Page 21: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/21.jpg)
Which parts goes together ?
partexportexport
importimportimport
partimport
? Contract
Name
TypeIdentity
Cardinality
CreationPolicy
Metadata
IsRecomposable
IsPrerequisit
Contract
Name
TypeIdentity
CreationPolicy
Metadata
![Page 22: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/22.jpg)
DEMO 3 - WIDGETS
![Page 23: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/23.jpg)
Metadata.
Widget Widget
Where to place that widget ?
![Page 24: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/24.jpg)
Export it. Metadata.
TwitterWidget
[Export(typeof(IWidget))][ExportMetadata("Location", WidgetLocation.Right)]public partial class TwitterWidget : ITwitterWidget{ [Import] public TwitterWidgetPresentationModel PresentationModel { get; set; }}
ExportIWidget
Put me on the right
![Page 25: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/25.jpg)
Import it. Metadata.
ShellPM
[Export]public class ShellPresentationModel{ [ImportMany] public Lazy<IWidget, IWidgetMetadata>[] Widgets { get; private set; }}
Import
Collection of Lazy IWidgets with IWidgetMetadata
![Page 26: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/26.jpg)
DEMO 4 – WIDGETS AND METADATA
![Page 27: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/27.jpg)
Export it. In a custom way.
TwitterWidget
[Export(typeof(IWidget))][ExportMetadata("Location", WidgetLocation.Right)]public partial class TwitterWidget : ITwitterWidget{ [Import] public TwitterWidgetPresentationModel Model { get; set; }}
ExportIWidget
Put me on the right
![Page 28: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/28.jpg)
Export it. In a custom way.
TwitterWidget
[Widget(WidgetLocation.Right)]public partial class TwitterWidget : ITwitterWidget{ [Import] public TwitterWidgetPresentationModel Model { get; set; }}
ExportIWidget
Put me on the right
![Page 29: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/29.jpg)
DEMO 5 – CUSTOM EXPORT
![Page 30: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/30.jpg)
Recomposition.
CompositionContainer
PartDefinitionPartDefinition PartDefinition
part
Compose()
partpart
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?
Catalog
PartDefinition
Catalog Catalog
![Page 31: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/31.jpg)
Recomposition.
ShellPM
[Export]public class ShellPresentationModel{ [ImportMany(AllowRecomposition = true)] public Lazy<IWidget, IWidgetMetadata>[] Widgets { get; private set; }}
Import
Collection of Lazy IWidgets with IWidgetMetadata
![Page 32: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/32.jpg)
DEMO 5 - RECOMPOSITION
![Page 33: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/33.jpg)
Stable composition.
part
part
requires
requires
missing
part
part
part
requires zero or more
requires
Parts with missing required imports are rejected.
![Page 34: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/34.jpg)
DEMO 6 – STABLE COMPOSITION
![Page 35: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/35.jpg)
• 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.
![Page 36: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/36.jpg)
Visual MEFX
![Page 37: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/37.jpg)
DEMO 7 – DEBUGGING MEF
![Page 38: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/38.jpg)
• Trim up your apps, break up your xaps!• Available only in Silverlight
DeploymentCatalog
![Page 39: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/39.jpg)
DEMO 8 – DEPLOYMENT CATALOG
![Page 40: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/40.jpg)
• 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
![Page 41: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/41.jpg)
• 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?
![Page 42: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/42.jpg)
DEMO 9 – MEF CONTRIB
![Page 43: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/43.jpg)
Extending MEF
MEF
Programming Models
Export Providers
![Page 44: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/44.jpg)
Export Providers.
ExportProvider
CompositionContainer
ExportProvider
Catalog
CatalogExportProvider
ExportProviderAggregateExportProvider
ExportProvider
CatalogExportProvider
ExportProvider
CompositionContainer
![Page 45: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/45.jpg)
• MEF ships with Attributed programming model
• Programming models are extensible
Programming models.
MEF Primitives
ComposablePartCatalogComposablePartDefinitionComposablePart
ExportDefinition
ImportDefinition
Export
ReflectionModelServices
PartDef ExportDef ImportDef
![Page 46: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/46.jpg)
• + 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
![Page 47: MEF Deep Dive by Piotr Wlodek](https://reader036.vdocuments.mx/reader036/viewer/2022062702/55496c06b4c90595518b4f04/html5/thumbnails/47.jpg)
• 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