MEF如何确定其导入的顺序?

发布于 2024-08-12 03:09:38 字数 516 浏览 4 评论 0原文

MEF 允许您通过使用 ImportMany 属性导入多个部件。它如何确定检索相关导出并将它们添加到您正在填充的枚举中的顺序?例如,如何导入必须按特定顺序触发的多个 IRules?我能想到的唯一方法是在 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();
    }
  }
}

MEF allows you to import multiple parts via the use of the ImportMany attribute. 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? The only way I can think of is to have an OrderValue property in IRule and sort manually:

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();
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2024-08-19 03:09:38

默认情况下,MEF 不保证导入的导出的任何顺序。然而,在 MEF 中,您可以通过使用一些元数据和自定义集合来进行一些排序。例如,您可以执行以下操作:

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; }
}

然后您将拥有一组按元数据排序的规则。您可以在 http://codepaste.net/ktdgoh 找到 OrderingCollection 示例。

By default MEF does not guarantee any order of the exports that get imported. However in MEF you can do some ordering by using some metadata and a custom collection. 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.

温柔少女心 2024-08-19 03:09:38

在 MEF 中实现这种排序的最佳方法是利用我们的元数据设施。您可以将自己的元数据附加到导出中,以便用于排序和过滤。元数据还允许您延迟部件的实例化,直到实际需要它们为止。此外,您还可以创建自定义导出属性,这提供了一种提供元数据的简洁方式。

有关如何定义元数据和自定义导出的信息,请检查此链接:链接文本

您还可以在我们的网站上找到主题MEF 论坛很有用。在其中,您将找到有关 AdaptingCollection 方法的讨论,该方法允许您使用应用元数据过滤器/顺序的自定义集合。

华泰
格伦

The best way to achieve this ordering in MEF is to utilize our metadata facilities. 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. Within you'll find a discussion about an AdaptingCollection approach which lets you use a custom collection which applies a metadata filter / order.

HTH
Glenn

段念尘 2024-08-19 03:09:38

您可以让规则按顺序相互导入(使用装饰器模式),但是每个规则都需要了解其前面的特定规则,这可能不是您想要的。

MEF 可以帮助您发现零件,之后如何使用它们取决于您。如果您想对零件进行排序,那就继续吧,这没有错!

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.

MEF is there to help you discover the parts, what you do with them afterwards is up to you. If you want to sort the parts then go ahead, there's nothing wrong with that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文