如何在不依赖 IOC 库的情况下定义通用导出?

发布于 2024-09-10 20:55:04 字数 389 浏览 7 评论 0原文

我正在开发一个需要使用公共服务定位器的库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-09-17 20:55:04

从文档中:

“库不应配置
容器”

“作为库或框架作者,
明白你不应该
将任何东西放入容器中 -
这是呼叫者的工作。允许
可供选择的应用程序编写者
他们想要什么容器。你需要
记录您需要什么服务
已注册,并且如果您正在使用
ServiceLocation.当前环境
容器。”

,我认为您不需要导出任何内容,只需记录运行时需要消耗的内容。并且可以选择使用 ServiceLocator.Current 接口来解决您需要的任何依赖项。

From the documentation:

"Libraries should not configure the
container"

"As a library or framework author,
understand that you should not be
putting anything into the container -
that's the job of your caller. Allow
the application writers to choose
whatever container they want. You need
to document what services you need
registered, and if you're using the
ServiceLocation.Current ambient
container."

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文