如何在不依赖 IOC 库的情况下定义通用导出?
我正在开发一个需要使用公共服务定位器的库(http://commonservicelocator.codeplex.com/ )提供通用的 IOC 支持,以便我们不与任何消费者发生冲突。显然,我不想在我的项目中直接引用 IOC 框架,但我不确定如何注释这些部分,以便各个框架可以找到它们。例如,MEF 通常通过使用 Export 注释查找类来工作,而 LinFu 通常通过使用 Implements 注释查找类来工作。这两个注释都是在各自的项目中定义的,要求我引用它们。
如何避免多次添加所有这些类并注释所有类?它当然不能很好地适应新的 IOC 框架,因为每个新框架的注释都需要重建。
I am working on a library which needs to make use of the common service locator (http://commonservicelocator.codeplex.com/) to provide generic IOC support in order that we don't conflict with any consumers. Obviously I don't want to reference the IOC frameworks directly in my project but I'm unsure as to how to annotate the parts such that they can be found by the various frameworks. For instance MEF usually works by finding classes with the Export annotation and LinFu by finding classes with the Implements annotation. Both these annotations are defined within their respective projects requiring that I reference them both.
How can I get away from adding all these classes and annotating all my classes multiple times? It certainly doesn't adapt well to new IOC frameworks as a rebuild would be required for each new framework's annotations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档中:
,我认为您不需要导出任何内容,只需记录运行时需要消耗的内容。并且可以选择使用 ServiceLocator.Current 接口来解决您需要的任何依赖项。
From the documentation:
So I don't think you need to export anything, just document what you need to consume in order to run. And optionally use the ServiceLocator.Current interface to resolve any dependencies you require.