MEF =可能会遇到的挫折? [英] MEF = may experience frustration?

查看:134
本文介绍了MEF =可能会遇到的挫折?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

正如我一直试图获得MEF在我的应用程序的工作,我在更多的更多的地方来,我只是不知道为什么,当我期望它它不是自动创建我的图书馆。我想这一切都回到了里德说大约需要MEF创造一切。所以现在,我需要用我的CandySettings XML阅读器类,但即使它ICandySettings酒店[导入]属性,它不会进口。首先,我发现了[导入]不能在静态工作,所以我改变了这一点。但在那之后它仍然没有奏效。我想这是因为我手动创建XML阅读器对象,MEF什么要我做的,而不是对[导入]的XML阅读器...这意味着我现在必须有一个接口为好。

As I've tried to get MEF working throughout my application, I'm coming across more an more places where I just don't get why it's not automatically creating my library when I expect it to. I think it all comes back to what Reed was saying about needing MEF to create everything. So right now, I have an XML reader class that needs to use my CandySettings, but even though its ICandySettings property has the [Import] attribute, it doesn't get imported. First I found out that [Import] doesn't work on statics, so I changed this. But after that it still didn't work. I think it's because I manually create the XML reader object, and what MEF wants me to do instead is to [Import] the XML reader... which means that I now have to have an interface for that as well.

这几乎就像使用IOC(或MEF,至少),它是一个全有或全无。你不能只是随意在这里和那里使用它,因为最终无论类要注入到属性也需要由MEF建立。

It's almost like using IoC (or for MEF, at least), it's an all-or-nothing affair. You can't just arbitrarily use it here and there, because ultimately whatever class you want to inject properties into also needs to be created by MEF.

请纠正我,如果我我错了!

Please correct me if I am wrong!

原帖

那么,它不是那么糟糕呢。 :)但我有疑问里德指出我在MEF作为一个潜在的替代IOC(到目前为止它看起来相当不错的)之后。

Well, it's not THAT bad yet. :) But I do have questions after Reed has pointed me at MEF as a potential alternative to IoC (and so far it does look pretty good).

考虑下面的模型:

Consider the following model:

正如你所看到的,我有一个应用程序,而这个应用程序使用插件(哎呀,错过了联想!)。无论是应用程序和插件需要类型CandySettings的对象,这是另一种装配发现的使用。

As you can see, I have an App, and this app uses Plugins (whoops, missed that association!). Both the App and Plugins require usage of an object of type CandySettings, which is found in yet another assembly.

我第一次尝试使用MEF的ComposeParts方法,但只有这样,我能得到这个工作是做这样的事情在插件代码

I first tried to use the ComposeParts method in MEF, but the only way I could get this to work was to do something like this in the plugin code.

var container = new CompositionContainer();
container.ComposeParts(this, new CandySettings());



但是,这并没有任何意义,因为我为什么要在创建CandySettings实例该插件?它应该是在App。但如果我把它放在应用程序代码,那么插件不会奇迹般地弄清楚,如何让在ICandySettings即使我使用[导入]在插件,并在CandySettings [出口]。 修改(可能是因为我要调用从App ComposeParts(),然后传递给它的插件?)

But this doesn't make any sense, because why would I want to create the instance of CandySettings in the plugin? It should be in the App. But if I put it in the App code, then the Plugin doesn't magically figure out how to get at ICandySettings, even though I am using [Import] in the plugin, and [Export] in CandySettings. EDIT (probably because I should be calling ComposeParts() from the App and then passing it the plugin?)

我做它的方式使用MEF的 DirectoryCatalog ,因为这允许插件,构造时,扫描所有的组件在当前文件夹,并自动导入标有[导入]属性的一切。所以它看起来像这样,并有可能在的每个的插件:

The way I did it was to use MEF's DirectoryCatalog, because this allows the plugin, when constructed, to scan all of the assemblies in the current folder and automagically import everything that is marked with the [Import] attribute. So it looks like this, and potentially in every plugin:

var catalog = new DirectoryCatalog(".");
var container = new CompositionContainer(catalog);
container.ComposeParts(this);

这完全的伟大工程,但我不禁想,这是不是MEF是如何打算要使用?

This totally works great, but I can't help but think that this is not how MEF was intended to be used?

推荐答案

绝招在这里,你想拥有MEF建立你的插件为您服务。

The "trick" here is that you want to have MEF create your plugins for you.

你会做到这一点的方法是让你的应用程序中撰写自己,用指定的插件类型:

The way you'll do this is to have your Application compose itself, with the Plugin types specified:

class PluginRepository
{
    [ImportMany(typeof(IPlugin))]
    IEnumerable<IPlugin> Plugins { get; set; }
}

如果你这样做,并有MEF撰写你的资源库级, MEF 将建设的对象。然后,它会自动撰写的,因为它构建了他们,所以 ICandySettings 将得到没有任何干预你组成。

If you do this, and have MEF Compose your "repository" class, MEF will construct the objects. It'll then automatically Compose those as it constructs them, so ICandySettings will get composed without any intervention for you.

你只需要手动撰写如果MEF并不构建为你的对象。

You only need to manually "compose" an object if MEF isn't constructing it for you.

这篇关于MEF =可能会遇到的挫折?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆