IoC 容器可以自动注册所有内容(非通用),无需配置(程序集到程序集)
我的想法是,我有一个带有大量接口的核心项目,还有带有实现的数据和服务项目(一切都是一对一),例如:
Core { IFooRepo, IBarRepo, IFooService, IBarService}
Data {FooRepo: IFooRepo, BarRepo : IBarRepo}
Service {FooService : IFooService, BarService : IBarService}
所以我想要
register(Core, Data);
register(Core, Service);
有很多 IoC 容器之类的东西,但我不知道他们中的哪一个可以做到这一点,或者更接近这个解决方案,有人知道吗?
the idea is that I have a Core project with lots of interfaces, also Data and Service project with implementations (everything 1-to-1), e.g.:
Core { IFooRepo, IBarRepo, IFooService, IBarService}
Data {FooRepo: IFooRepo, BarRepo : IBarRepo}
Service {FooService : IFooService, BarService : IBarService}
so I would like something like
register(Core, Data);
register(Core, Service);
there are lots of IoC containers and I don't know which one of them can do this, or is closer to this solution, anybody knows ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在谈论自动注册。许多 IoC 容器都支持这一点。
结构图
http://structuralmap.net/structuralmap/ScanningAssemblies.htm
温莎城堡(参见第二部分底部文章页面)
http://www.code-magazine.com/article.aspx?quickid= 0906051
Autofac
http://code.google.com/p/autofac/wiki/Scanning
忍者
看起来你可以通过 Kernel.Scan() 来完成,尽管我找不到文档。 (服务器不可用。)
如何在不引用的情况下使用 Ninject Conventions 扩展程序集(或其中的类型)
最后我发现,Unity 不支持自动注册,尽管最近的版本可能有所改变。
更新:感谢毛里西奥注意到我错误地将所需的功能识别为自动接线。更正并更新了链接。
You are talking about auto-registration. Many IoC containers support this.
StructureMap
http://structuremap.net/structuremap/ScanningAssemblies.htm
Castle Windsor (see bottom of 2nd page of the article)
http://www.code-magazine.com/article.aspx?quickid=0906051
Autofac
http://code.google.com/p/autofac/wiki/Scanning
Ninject
Looks like you can do it via Kernel.Scan(), though I couldn't find docs. (Server was unavailable.)
How to use Ninject Conventions extension without referencing Assembly (or Types within it)
Last I looked, Unity did not support auto-registration, though that may have changed with a recent release.
UPDATE: Thanks to Mauricio for noticing that I incorrectly identified the desired feature as auto-wiring. Corrected and updated the links.
在 Autofac 中,实现此目的的最简单方法是:
现在不会对来自核心程序集的服务接口进行选择。如果这真的很重要(可能不应该),那么按照以下方式改变上述注册:
干杯,
缺口
In Autofac the simplest way to achieve this is:
Now this will not be selective about the service interfaces being from the Core assembly. If this really matters (it probably shouldn't) then vary the above registrations along these lines:
Cheers,
Nick
Windsor 使您可以轻松地将类注册为它们公开的接口,可以全部注册,也可以有选择地注册。 (请参阅文档)。
它不支持您的场景(过滤实现的接口以仅包含来自特定程序集的接口),但是(对于 Windsor 中的所有内容)您可以使用一个挂钩轻松实现这一点。
Windsor lets you easily register classes as interfaces they expose, either all of them or, selectively. (see the documentation).
It does not have OOTB support for your scenario (filtering implemented interfaces to only include those from specific assembly) but (as for everything in Windsor) there's a hook you can use to easily have that.