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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
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:
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.