MEF系列四:声明导入(Imports)

导入声明

组合部件声明导入 [System.ComponentModel.Composition.ImportAttribute]属性。类似于导出,有几种不同的方法即通过字段,属性和构造函数参数。

导入属性

为属性导入一个值,用[System.ComponentModel.Composition.ImportAttribute]修饰属性,如下面的例子导入一个IMessageSender

class Program
  {
    [Import]
    public IMessageSender MessageSender { get; set; }
  }

构造参数

你也能通过构造参数指定导入。这意味着替换了为每个导入添加属性,你为每个导入添加构造参数,根据下面的步骤

1、为通过MEF使用的构造函数添加[System.ComponentModel.Composition.ImportingConstructorAttribute]属性

2、为每个导入构造函数添加参数


如下例,Program的构造函数中导入一个message sender 

class Program
  {
    [ImportingConstructor]
    public Program(IMessageSender messageSender) 
    {
       ...
    }
  }

导入参数


有几种不同方法在构造函数中定义导入

1、隐式导入-通过默认的组合容器对象将会使用参数的类型去定义契约,如下面的例子,IMessageSender
1. Implied import - By default the container will use the type of the parameter to identify the contract. For example in the code below, the IMessageSender contract will be used.

class Program
  {
    [ImportingConstructor]
    public Program(IMessageSender messageSender) 
    {
    }
  }

2. Explicit import - If you want to specify the contract to be imported add an [System.ComponentModel.Composition.ImportAttribute]  attribute to the parameter. 

Field Imports

MEF also supports importing values directly to fields.

class Program
  {
    [Import]
    private IMessageSender _messageSender;
  }

Note: note that importing or exporting private members (fields, properties and methods) while supported in full trust is likely to be problematic on medium/partial trust.

Optional imports

MEF allows you to specify that an import is optional. When you enable this, the container will provide an export if one is available otherwise it will set the import to  Default(T) . To make an import optional, set  AllowDefault=true  on the import as below.

[Export]
public class OrderController {
  private ILogger _logger;

  [ImportingConstructor]
  public OrderController([Import(AllowDefault=true)] ILogger logger) {
    if(logger == null)
      logger = new DefaultLogger();
    _logger = logger;
  }
}

OrderController optionally imports a logger. If the logger is not present, it will set it's private  _logger  to a new  DefaultLogger  instance otherwise it will use the imported logger.

Importing collections

In addition to single imports, you can import collections with the ImportMany attribute. This means that all instances of the specific contract will be imported from the container.

MEF parts can also support recomposition. This means that as new exports become available in the container, collections are automatically updated with the new set. For example below the Notifier class imports a collection of IMessageSender. This means if there are 3 exports of IMessageSender available in the container, they will be pushed in to the Senders property during compositon. 

public class Notifier 
 {
    [ImportMany(AllowRecomposition=true)]
    public IEnumerable<IMessageSender> Senders {get; set;}

    public void Notify(string message) 
    {
      foreach(IMessageSender sender in Senders)
      {
        sender.Send(message);
      }
    } 
  }

IPartImportsSatisfiedNotification

In some situations it may be important for your class to be notified when MEF is done with the import process for your class instance. If that's the case implement the [System.ComponentModel.Composition.IPartImportsSatisfiedNotification]  interface. This interface has only a single method: OnImportsSatisfied, which is called when all imports that could be satisfied have been satisfied.

public class Program : IPartImportsSatisfiedNotification
 {
    [ImportMany]
    public IEnumerable<IMessageSender> Senders {get; set;}

    public void OnImportsSatisfied() 
    {
      // when this is called, all imports that could be satisfied have been satisfied.
    } 
  }


http://mef.codeplex.com/wikipage?title=Declaring%20Imports&referringTitle=Guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值