简体   繁体   中英

Is it possible to have plugin “bundles” with MEF?

I'm currently extending an existing WPF application to make use of various extensions. Primarily to have a common UI platform, which loads plugins that are developed by the specific teams that use it. Some of these extensions might depend on each other (eg a tree control plugin and a canvas where you can drag on stuff from the tree). Is this possible (and advisable) to do with MEF?

I thought of doing this via a common Interface that is exported by the plugins that belong together, but I'm unsure if this is a good practice for MEF.

Since you're using MEF to manage your dependencies, describe them with following to dependency inversion principle.

If one of your plugins requires another one to work properly, just import its implementation:

public interface ITreePlugin {}
public interface ICanvasPlugin {}

[Export(typeof(ICanvasPlugin)]
public class CanvasPlugin : ICanvasPlugin 
{
    // treePlugin must be imported
    [ImportingConstructor]
    public CanvasPlugin(ITreePlugin treePlugin) {}

    // ...
}

This sample allows to put any ITreePlugin implementation in container. Moreover, it requires ITreePlugin implementation to present in container (note, that you could make optional imports). This is your "plugin bundle".

Do not couple particular plugin implementations together, because this makes MEF usage meaningless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM