如何将基本接口列表注入 class
How to inject list of base interfaces to class
我有一个程序,我有一些带有自定义接口的服务,但它们都继承自 IService
。这些服务在 Simple Injector 容器中以其自定义接口注册为单例,但在一个主要 class 中,我希望它们全部注入到带有 IEnumerable<IService>
的构造函数中。这不起作用,因为 IEnumerable<IService>
未注册。我的问题是如何配置容器以允许在构造函数中检索它们——我可以手动解析类型,但我应该避免这种反模式。
// sample service (one of many)
public interface IHttpServerService : IService
...
_container.RegisterSingleton<IHttpServerService, HttpServerService>();
...
public Controller(IEnumerable<IService> services)
...
只需添加:
container.RegisterSingleton<IHttpServerService, HttpServerService>();
container.Collection.Register<IService>(
typeof(IHttpServerService),
// more implementations here
);
我有一个程序,我有一些带有自定义接口的服务,但它们都继承自 IService
。这些服务在 Simple Injector 容器中以其自定义接口注册为单例,但在一个主要 class 中,我希望它们全部注入到带有 IEnumerable<IService>
的构造函数中。这不起作用,因为 IEnumerable<IService>
未注册。我的问题是如何配置容器以允许在构造函数中检索它们——我可以手动解析类型,但我应该避免这种反模式。
// sample service (one of many)
public interface IHttpServerService : IService
...
_container.RegisterSingleton<IHttpServerService, HttpServerService>();
...
public Controller(IEnumerable<IService> services)
...
只需添加:
container.RegisterSingleton<IHttpServerService, HttpServerService>();
container.Collection.Register<IService>(
typeof(IHttpServerService),
// more implementations here
);