是否有可能以现有的实例到MEF插件注入?有可能、插件、实例、MEF

2023-09-07 14:38:21 作者:被你需要

我们正在创建使用MEF支持插件的应用程序。我们确定是什么类型的插件的用户能够创建,并要使用依赖注入提供这种类型的插件提供它所需要的数据。

We are creating an application which supports plugins using MEF. We are determining what type of plugins the user is able to create, and want to use dependency injection to provide this type of plugin with the data it needs.

例如,我们做一个插件,能够显示一个列表。为了实现这一点,就需要对数据的列表将显示类型的IRepository的现有实例

For example, we make a plugin that is able to display a list. To achieve this, it needs the existing instance of the IRepository for the type of data the list will display.

该IRepository创建其他地方的DataContext类,所以我们无法让MEF自己创建IRepository的一个实例。

The IRepository is created somewhere else in a datacontext class, so we are unable to let MEF itself create an instance of the IRepository.

我的想法是在IRepository的现有实例注入到通过importingconstructor插件,但是,对于这个工作,我需要做的已经实例IRepository知道MEF,我一直无法弄清楚如何做到这一点。任何帮助将是AP preciated。

My idea is to inject the existing instance of the IRepository into the plugin via the importingconstructor, however for this to work I need to make the already instantiated IRepository known to MEF, and I haven't been able to figure out how to do it. Any help would be appreciated.

推荐答案

最简单的方法就是组成一个现有的值在容器中,如:

The easiest way is to compose an existing value in the container, e.g.:

var repo = // Create repo
container.ComposeExportedValue<IRepository>(repo);

但是的,这将只允许1个实例的 IRepository 来存在的,因为它不会给你直接控制 ComposablePart 创建的。如果您想要更为精细的控制,你可以使用 CompositionBatch 来很大的影响:

But this will only allow 1 instance of the IRepository to exist, because it doesn't give you direct control over the ComposablePart that is created. If you want more fine grained control, you can use a CompositionBatch to great effect:

var batch = new CompositionBatch();
var repo = // Create repo

var repoPart = batch.AddExportedValue<IRepository>(repo);
container.Compose(batch);

// repo will now be injected on any matching [Import] or [ImportingConstructor]

和以后:

var batch2 = new CompositionBatch(null, new[] { repoPart });
var repo2 = // Get new repo

var repo2Part = batch2.AddExportedValue<IRepository>(repo2);
container.Compose(batch2);

由于我有机会获得 ComposablePart 的批次提供例如,我可以稍后将其删除。有进口的属性少部分的其他方法,一般是通过财产出口:

Because I have access to the ComposablePart instance provided by the batch, I can remove it later on. There are other ways of importing attribute-less parts, generally through property exports:

[Export(typeof(IRepository))]
public IRepository Repository
{
    get { return CreateRepository(); }
}

但是,这当然需要你能够创造你的资料库,在作文的时间,这可能会或可能无法的一个实例。

But that of course would require you to be able to create an instance of your repository at composition time, which may or may not be possible.

最后,还有使用替代的编程模型的选项。在MEF默认的(也是最常见的)是的由于的编程模型,即你使用 [导出] [导入] 属性来控制您的作品,但在MEFContrib(以及即将于MEF2)是使用能力的注册的编程模型,从而部分是基于类似于大多数的机构组成其他的IoC容器。

Lastly, there is the option to use an alternative programming model. The default (and most common) in MEF is the attributed programming model, whereby you utilise [Export] and [Import] attributes to control your composition, but in MEFContrib (and forthcoming in MEF2) is the ability to use a registration programming model whereby parts are composed based on a mechanism similar to most other IoC containers.

 
精彩推荐
图片推荐