Anyone with MEF experience?

As the topic says, anyone here with some MEF experience?

I played with it a little.

I’m looking for some examples on how i can populate the main app’s menu with menuitems from the plugins. Any idea?

I would put a method/property in the interface of the plugin that would return an IEnumerable containing items for the menus. Those items would also implement a common interface or class (IMenuItem). Once you get those items, you create you views and models based on that (wpf or winforms) and that’s it.

interface IPlugin
  IEnumerable<IMenuItem> MainMenuItems {get;}

interface IMenuItem
  string Header {get;}

@ Eric : could you please send me a mail : becafuel “at” lsp-fr dot com

MEF rocks! Are you looking for specifics about how to implement MEF (attribute syntax) or the best way to implement a dynamic menu (and then MEF it)? godfather’s reply is a good start. I would add that the answer ultimately depends on how your main app menu is built.

For example, I work on a WPF app that uses a custom control to build the menu with a XAML resource file at runtime. Each menu item is bound to a command. Another developer needed a way to add menu items (with commands) to kick off his external tools with context sensitive data from my app, without having to pester me to refactor/rebuild. MEF worked perfectly. He can update his library of menu commands at any time and just drop it into a folder in my app. All we had to do was agree on the interface. Very slick.

Bec a Fuel, Mail sent :wink:

I was working at the end of last year on MEF, too. My goal was dynamic loading of drivers for astronomical software : the client application only has to use a (publicly exposed) “selector” that will load existing drivers of different kinds : focusers, filterwheels, cameras, and so on…

It’s giving something as simple as this (for the client app, that is, because it’s obviously not the case behind the scene…) :

AstroDevicesNet.Repository.Focuser FocuserRepository = new AstroDevicesNet.Repository.Focuser(@ "c:\dev\devtmp\DriversMEF");
AstroDevicesNet.Repository.Camera CameraRepository = new AstroDevicesNet.Repository.Camera(@ "c:\dev\devtmp\DriversMEF");

        ADNFocuser Focuser1;
        ADNCamera Camera1;

        Focuser1 = FocuserRepository.Selector();
        Camera1 = CameraRepository.Selector(@ "\Other directory than default");
		Camera1.ExpTime = 16;

The code in the attached zip archive is functionnal, although I was still trying different things with it such as concurent accesses to multiple devices inside a single driver. So you may find some unused or not useful code in it.

Link :

This code is also part of a wider open-source project currently being developped by other friends. So if you use (big) parts of it, it would be nice to quote the “AstroDevices.Net” project. Not a huge text, of course, a single line is enough. :wink:

Notice : since it’s still work in progress, I wanted to keep it private, hence my claim for a mail. But since it’s also an open-source project, I see no real reason to keep it “secret” :hand:

If anyone has any suggestions for resources for learning MEF, please post them. This is one of the technologies I have to learn in the next few weeks. If seems there’s not a single book written on the subject yet so i assume all the knowledge is online.

Don’t know if you have seen this:

I’ve learned it the hard way, by using online examples and all I could find about it on the net ???

I haven’t started down the MEF trail yet but hope to start by mid next week when I finish my current book. Sounds like you guys have already figured it out. So, just wanting to save some time since I’m sure you have already found the best resources :wink:

Thanks, godfather.

hi Ericsan and the other

Concerning MEF, I’ve taking at look at it and use it for a piece of code (can’t publish it because it’s company stuff, sorry)…

The application was a simulation tool.

Our goal was to make almost everypart of the software as a plug-in.

As the main architect for this solution, I choose MEF to achieve this “maybe” more easily than by doing my own plug-in system (I’ve already done that kind of architecture and code before).

To achieve our goal, I defined many kind of plug-in. Some where only “process” plug-in as some where devoted to GUI stuff. Some where both.

The main part of the main software was about the loading of the plug-in and the ability to integrate in the window system of our application what the plug-in could provide.

For instance, we had one interface defining that the plug-in was offering extra menu to be integrated in the application. One method of the plug-in was something like that (pseudo code)

Menu GetPluginMenu();

The main application has only to check if the plug-in was offering such a feature and then get the menu and add it to the main menu.

I’ve also defined interface and plug-in for “Log” system, Simulation Plug-in.

I also defined a “Setting plug-in” which where able to gather the setting of each plug-in and create dynamicaly a form by associating “type” of the setting to display with “controle” to use to do that (I’ve defined a xml file where we could fine the assembly where to get the right controle to display for the right type (could be “base” type or “complexe” type").

By doing so (for the setting plug), we were able to integrate different kind of display for same control just by changing the XML control mapping file and by providing, of course, the assembly containing these special controls.

MEF helped me “a lot” to define the contract and to ease my pain in loading all the plug-in.

This is all I can say about this use of the MEF in a “plug-in” context.

Hope you will find it useful and not to “hard and long” to read.


I’m working on a personal MEF project and its getting into a fairly useable state. Still need to figure out many things, but if you want, I can host the project on my server for you to download.

The Monz,

thanks for your explanation.

De rien, une fois :slight_smile:

It sounds like MEF is useful. I’m starting a new job in a couple weeks and MEF is one of the techs they said they wanted to implement soon. I don’t really know the details of what they have in mind but I want to get somewhat familiar before I get there.

Eric, that would be awesome. Real world examples are far too rare most of the time. I’d love to look at what you’ve done.


@ ianlee74 - the reason you won’t find a dedicated book on the topic is because it is so elegant in its simplicity that there is not much to write about. MEF started as a codeplex project. Word quickly spread and a couple of versions later it became part of the .NET framework.

If you passed 40 when learning new stuff, I can tell you that MEF is not that simple :-[

I highly recommend to take a look at Prism. I use it at work and I love it.