简体   繁体   English

托管可扩展性框架(MEF),为什么需要Imports?

[英]Managed Extensibility Framework (MEF), why are Imports needed?

I wonder what the use of Import/ImportMany decorations are good for? 我想知道Import / ImportMany装饰的用途是什么? I played with MEF and manage everything through the CompositionContainer. 我玩MEF并通过CompositionContainer管理所有内容。 I decorated the exports with a custom MetaDataAttribute that derives from ExportAttribute. 我使用派生自ExportAttribute的自定义MetaDataAttribute修饰了导出。 When I try to access plugin instances I can lazily access the meta data and the plugin implementation through Container.GetExports<T, IMetaDataAttribute>() . 当我尝试访问插件实例时,我可以通过Container.GetExports<T, IMetaDataAttribute>()懒惰地访问元数据和插件实现。

So, why would I need to bother with Import decorations? 那么,为什么我需要打扰导入装饰? I understand the core of MEF is the CompositionContainer and that is what I should really care about. 我理解MEF的核心是CompositionContainer,这是我真正应该关心的。 But most examples on the web pass in Import decorated object instances. 但Web上的大多数示例都在Import装饰对象实例中传递。 Why is that and what added value do they provide? 为什么它们提供了什么增值?

Here is an example of how I access my meta data and actual plugins: 以下是我如何访问元数据和实际插件的示例:

    public static IEnumerable<IPluginAttributeView> GetMetaData<T>()
    {
        return Container.GetExports<T, IPluginAttributeView>().Select(e => e.Metadata);
    }

    public static T GetPlugin<T>(string pluginName) where T : class
    {
        var plugins = Container.GetExports<T, IPluginAttributeView>();
        var pluginByName = plugins.Where(e => e.Metadata.PluginName.Equals(pluginName)).FirstOrDefault();

        return pluginByName.Value;
    }

Am I missing something or lack understanding? 我错过了什么或缺乏理解吗? Please help me understand. 请帮我理解。

The import attributes (and their equivalent RegistrationBuilder methods in MEF2) are very useful because they offer you automatic dependency injection. 导入属性(及其在MEF2中的等效RegistrationBuilder方法)非常有用,因为它们为您提供自动依赖注入。 The container handles this for you. 容器为您处理此问题。 You can have many types decorated with export attributes and wiht members decorated with one of the import attributes and let the container compose them when you need them. 您可以使用导出属性修饰许多类型,并使用其中一个导入属性修饰成员,并在需要时让容器组成它们。 So you just use the container to get the exports that no other type is importing. 因此,您只需使用容器来获取其他类型无法导入的导出。 All the rest is handled for you by MEF. 所有其余的都由MEF为您处理。 If you avoid the import attributes then you will have to inject all dependencies yourself. 如果您避免使用import属性,那么您必须自己注入所有依赖项。

If you look at most of the MEF examples out there you will note that most of them use the import attributes extensively. 如果您查看大多数MEF示例,您会注意到它们中的大多数都广泛使用了导入属性。 There are people that use MEF and don't even know that the GetExportXXX methods are provided by the CompositionContainer . 有些人使用MEF,甚至不知道CompositionContainer提供了GetExportXXX方法。 It is up to you of course to decide what is best suited for your application(s). 当然,您可以自行决定最适合您的应用的产品。 In your case (plug-in manager), the GetExports method might be enough. 在您的情况下(插件管理器), GetExports方法可能就足够了。 If you decide to add dependencies between plug-ins then the import attributes will become very valuable and your plug-in manager much simpler. 如果您决定在插件之间添加依赖项,则导入属性将变得非常有价值,并且插件管理器更加简单。 Another nice feature of MEF for plug-in managers is recomposition, which as far as I know is available only via the import attributes (or MEF2's RegistrationBuilder ). 插件管理器MEF的另一个不错的功能是重构,据我所知,只有通过导入属性(或MEF2的RegistrationBuilder )才能使用。 This can add great value to your plug-in aware app. 这可以为您的插件识别应用程序增添巨大价值。 Either way you still have the extensibility feature that MEF is supposed to provide. 无论哪种方式,您仍然具有MEF应该提供的可扩展性功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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