简体   繁体   English

MEF如何确定其进口的顺序?

[英]How does MEF determine the order of its imports?

MEF allows you to import multiple parts via the use of the ImportMany attribute. MEF允许您通过使用ImportMany属性导入多个部件。 How does it determine the order in which it retrieves the relevant exports and adds them to the enumerable you are populating? 它如何确定检索相关导出的顺序并将它们添加到您正在填充的可枚举中? For example, how would I import multiple IRules that had to fire in a particular order? 例如,如何导入必须以特定顺序触发的多个IRules? The only way I can think of is to have an OrderValue property in IRule and sort manually: 我能想到的唯一方法是在IRule中使用OrderValue属性并手动排序:

public class Engine
{
  [ImportMany]
  public IEnumerable<IRule> Rules { get; set; }

  public void Run()
  {
    // ...
    // Initialise MEF
    // ...

    //
    // Do I need to manually order Rules here?
    //

    foreach (IRule rule in Rules)
    {
      // Must execute in a specific order
      rule.Execute();
    }
  }
}

By default MEF does not guarantee any order of the exports that get imported. 默认情况下,MEF不保证导入的导出的任何顺序。 However in MEF you can do some ordering by using some metadata and a custom collection. 但是在MEF中,您可以使用一些元数据和自定义集合进行一些排序。 For example you can do something like: 例如,您可以执行以下操作:

public interface IRule { }

[Export(typeof(IRule))]
[ExportMetadata("Order", 1)]
public class Rule1 : IRule { }

[Export(typeof(IRule))]
[ExportMetadata("Order", 2)]
public class Rule2 : IRule { }

public interface IOrderMetadata
{
    [DefaultValue(Int32.MaxValue)]
    int Order { get; }
}

public class Engine
{
    public Engine()
    {
        Rules = new OrderingCollection<IRule, IOrderMetadata>(
                           lazyRule => lazyRule.Metadata.Order);
    }

    [ImportMany]
    public OrderingCollection<IRule, IOrderMetadata> Rules { get; set; }
}

Then you will have a set of rules that are ordered by the metadata. 然后,您将拥有一组按元数据排序的规则。 You can find the OrderingCollection sample at http://codepaste.net/ktdgoh . 您可以在http://codepaste.net/ktdgoh找到OrderingCollection示例。

The best way to achieve this ordering in MEF is to utilize our metadata facilities. 在MEF中实现此排序的最佳方式是利用我们的元数据设施。 You can attach your own metadata to exports which you can use for ordering and filtering. 您可以将自己的元数据附加到可用于排序和过滤的导出。 Metadata also allows you to delay instantiation of parts until they are actually needed. 元数据还允许您延迟部件的实例化,直到实际需要它们为止。 Additionally you can create custom Export attributes which provide a nice clean way of providing the metadata. 此外,您可以创建自定义导出属性,这些属性提供了一种提供元数据的简洁方法。

Check this link for information on how to define metadata and custom exports: link text 有关如何定义元数据和自定义导出的信息,请查看此链接: 链接文本

You may also find this thread on our MEF forums useful. 您也可以在我们的MEF论坛上找到这个主题。 Within you'll find a discussion about an AdaptingCollection approach which lets you use a custom collection which applies a metadata filter / order. 在其中您将找到有关AdaptingCollection方法的讨论,该方法允许您使用应用元数据过滤器/订单的自定义集合。

HTH Glenn HTH Glenn

You could have the rules import each other in order (using the Decorator pattern), but then each rule will need to know about the specific rule that precedes it, which probably isn't what you want. 您可以让规则按顺序相互导入(使用Decorator模式),但是每个规则都需要知道它之前的特定规则,这可能不是您想要的。

MEF is there to help you discover the parts, what you do with them afterwards is up to you. MEF可以帮助您发现零件,之后您使用它们取决于您。 If you want to sort the parts then go ahead, there's nothing wrong with that! 如果你想对零件进行分类然后继续,那就没有错!

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

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