What are some best practices for using MEF in your code? Are there any pitfalls to take into account when starting your extensible application? Did you run into anything you should have known earlier?
I'm in the midst of building a fully fledged extensible application on MEF (and using WPF with the MVVM pattern). I took the basic application framework that I built and open sourced it as SoapBox Core. I also published a demo based on SoapBox Core over at Code Project: Building an Extensible Application with MEF, WPF, and MVVM.
I'm not sure if using MVVM applies to you, but if it does, then there's a lot you might learn by looking at the implementation of MVVM with MEF. Particularly the way it imports Views.
As far as best practices... I created a nested hierarchy of extensions (so the basic module is called Host, and all it does is compose the application and import a few basic extensions). Then those extensions expose other extension points and the application kind of builds itself when you run it (a cross between composition and extensions).
To keep everything straight, I put the extension hierarchy into a set of static classes. For instance, here are all the extension points that the core framework provides:
namespace SoapBox.Core.ExtensionPoints { public static class Host { public const string Styles = "ExtensionPoints.Host.Styles"; public const string Views = "ExtensionPoints.Host.Views"; public const string StartupCommands = "ExtensionPoints.Host.StartupCommands"; public const string ShutdownCommands = "ExtensionPoints.Host.ShutdownCommands"; } public static class Workbench { public const string ToolBars = "ExtensionPoints.Workbench.ToolBars"; public const string StatusBar = "ExtensionPoints.Workbench.StatusBar"; public const string Pads = "ExtensionPoints.Workbench.Pads"; public const string Documents = "ExtensionPoints.Workbench.Documents"; public static class MainMenu { public const string Self = "ExtensionPoints.Workbench.MainMenu"; public const string FileMenu = "ExtensionPoints.Workbench.MainMenu.FileMenu"; public const string EditMenu = "ExtensionPoints.Workbench.MainMenu.EditMenu"; public const string ViewMenu = "ExtensionPoints.Workbench.MainMenu.ViewMenu"; public const string ToolsMenu = "ExtensionPoints.Workbench.MainMenu.ToolsMenu"; public const string WindowMenu = "ExtensionPoints.Workbench.MainMenu.WindowMenu"; public const string HelpMenu = "ExtensionPoints.Workbench.MainMenu.HelpMenu"; } } public static class Options { public static class OptionsDialog { public const string OptionsItems = "ExtensionPoints.Options.OptionsDialog.OptionsItems"; } } }
So if you wanted your extension to add something to the file menu, you would export something that implements IMenuItem with contract name SoapBox.Core.ExtensionPoints.Workbench.MainMenu.FileMenu
Each extension has an "ID" which is just a string identifier. These existing IDs are defined in another hierarchy:
namespace SoapBox.Core.Extensions { public static class Workbench { public static class MainMenu { public const string File = "File"; public const string Edit = "Edit"; public const string View = "View"; public const string Tools = "Tools"; public const string Window = "Window"; public const string Help = "Help"; public static class FileMenu { public const string Exit = "Exit"; } public static class ViewMenu { public const string ToolBars = "ToolBars"; } public static class ToolsMenu { public const string Options = "Options"; } } } }
As you can see FileMenu already contains an Exit extension (that is pre-programmed to close the app). If you wanted to add an extension to the File menu you probably want it to appear before the Exit menu item. IMenuItem inherits from IExtension, which has two properties:
So your extension would return SoapBox.Core.Extensions.Workbench.MainMenu.FileMenu.Exit for InsertRelativeToID, and would return Before for the BeforeOrAfter property (an enumeration). When the workbench imports all the File menu extensions, it sorts everything based on these IDs. In this way, the later extensions insert themselves relative to the existing extensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With