简体   繁体   English

autofac多个服务(相同的界面-提供它的多个插件)如何实现?

[英]autofac multiple services (same interface - multiple addons providing it) how to implement?

I have a service interface, lets say IAddonServiceX which is implemented by many, but not all addons to a system. 我有一个服务接口,可以说IAddonServiceX,它由许多(但不是全部)插件实现。 I want to have an IEnumerable so that I could iterate and execute methods for every addon that registered this service? 我想拥有一个IEnumerable,以便我可以为注册此服务的每个插件迭代并执行方法? Ho can this be done with autofac? 可以用autofac完成吗?

Autofac has implicit support for it , so you just need register your implementations with Autofac对此提供了隐式支持 ,因此您只需要向其注册实现即可

builder.RegisterType<Impl1>.As<IAddonServiceX>();    
builder.RegisterType<Impl2>.As<IAddonServiceX>();

And when you are resolving an IEnumberable<IAddonServiceX> with container.Resolve<IEnumberable<IAddonServiceX>> or using IEnumberable<IAddonServiceX> in your constructor then Autofac will provide you all the implementations. 当您使用container.Resolve<IEnumberable<IAddonServiceX>>解析IEnumberable<IAddonServiceX>或在构造函数中使用IEnumberable<IAddonServiceX> ,Autofac将为您提供所有实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM