MEF 的接口应该放在哪里?
在组织项目时,我应该将 MEF 中使用的提供者接口放在哪里? 目前,我只是将它们与其他所有项目放在同一个项目中,但似乎我可能需要将它们提取到一个单独的 dll 中,这样它就是一个非常小的 dll,并且很容易被尝试编写扩展的其他人链接到。 对此有什么好的做法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与任何插件/扩展模型一样,您应该将“契约”(插件作者应实现的接口)放在与应用程序分开的程序集中。
这样,您就可以将该程序集提供给插件作者,而无需向他们提供整个应用程序 - 如果它是需要单独许可的商业应用程序,则非常有用。
MEF Preview 5 引入了导出接口的功能(即向接口添加 [Export] 属性),以便自动导出该接口的任何实现者。 这意味着插件作者甚至不需要了解 MEF - 他们只需实现您的接口,并且它们自动成为 MEF 扩展。
As with any plug-in/extension model, you should put your "contracts" (the interfaces a plug-in author should be implementing) in an assembly separate from your application.
That way you can make that assembly available to plug-in authors without having to give them the entire application - useful if it's a commercial app that you need to license separately.
MEF Preview 5 introduces the ability to export an interface (ie add an [Export] attribute to an interface) such that any implementor of that interface is automatically exported. That means that plug-in authors don't even need to know about MEF - they just implement your interface and they're automatically a MEF extension.
实际上,.NET 4.0 中有一个称为类型等效的新功能可以实现此目的。 通过此功能,您可以在不同的协定程序集中拥有两个不同的接口,告诉 CLR 它们是相同的。 由于它是低级别的,MEF 可以很好地使用它。
一些注意事项:
您可以在此处阅读更多相关信息:http://msdn.microsoft.com/en-us/library/dd997297(VS.100).aspx。文档会说它适用于 COM,但您可以将其用于托管代码。以及。
Actually there is new feature in .NET 4.0 called type equivalence that can accomplish this. With this feature you can haave two different interfaces in different contract assemblies that tell the CLR they are the same. Because it is low-level, MEF can work with it fine.
A few caveats:
You can read more about it here: http://msdn.microsoft.com/en-us/library/dd997297(VS.100).aspx. The documentation will say it's for COM, but you can use it for managed code as well.
最初 MEF 打算实现鸭子类型,这意味着您不需要通用程序集,但显然这被证明太困难了。
我将它们全部放在一个公共程序集中,以及一些可用于帮助实现接口的有用的抽象基类。
Originally MEF was going to implement duck typing, which would mean you wouldn't need a common assembly, but apparently this proved too difficult.
I'm putting them all in a common assembly, along with some useful abstract base classes that can be used to help implement the interfaces.
我也有同样的问题,想看一个示例,其中在一个项目中定义合同,在其他项目中定义多个实现,以及一个单独的消费者项目,该项目使用合同并具有扩展文件夹,可以在其中简单地复制实现 dll 并可用于消费者应用程序无需任何代码更改。 所以我尝试编写一个简单的 Hello World 类型的应用程序并发布在我的博客上。 希望您会发现它有用。 我还发布了源代码(C#)。
http://ppsinfo.blogspot.com/2009/11/托管扩展性框架-mef.html
I was also having same question and wanted to see an example where Contracts are defined in one project, multiple implementations are defined in other projects and a separate consumer projects which uses contract and has extension folder where implementation dll's can be simply copied and is available to the consumer application without any code changes. So I tried writing a simple Hello World kind of application and posted at my blog. Hope you might find it useful. I have also posted the source code (in C#).
http://ppsinfo.blogspot.com/2009/11/managed-extensibility-framework-mef.html