SimpleInjector:向InstanceCreator注册集合 [英] SimpleInjector : Register collection with InstanceCreator

查看:45
本文介绍了SimpleInjector:向InstanceCreator注册集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个集合及其instanceCreator一起注册.我无法找到接受instanceCreator的 Container.Collection.Register 方法的任何重载.如果我尝试运行循环并使用 Container.Register(instanceCreator,生活方式)多次注册一个类型及其instanceCreator,我将无法执行此操作,因为我收到了该类型无法执行的错误被多次注册.

I'm trying to register a collection along with its instanceCreator. I wasn't able to find any overload for the Container.Collection.Register method which accepts an instanceCreator. If i try to run a loop and register a type along with its instanceCreator multiple times using the Container.Register(instanceCreator, lifestyle), i'm unable to do it because i get the error that type cannot be registerd multiple times.

基本上我想做的是:

foreach(var item in items){
 Container.Register<ILogWriter>(instanceCreator, lifestyle)
}

我有 ILogWriter 的多个实现,每个实现都有各自不同的配置,需要在构造函数中传递它们,因此我需要使用instanceCreator重载.

I have multiple implementations of ILogWriter and each have their own different configurations which need to passed in the constructor, hence i need to use the instanceCreator overload.

TIA

推荐答案

Container.Collection.Register 用于立即注册整个集合.例如,通过使用批注册并提供 Assembly 实例的列表或 Type 实例的列表.

Container.Collection.Register is meant for registering the whole collection at once. For instance by using batch-registration and supplying a list of Assembly instances, or a list of Type instances.

另一方面,您正在寻找的是为实例集合注册一个项目.这是通过 Container.Collection.Append 重载完成的:

What you are looking for, on the other hand, is registering a single item for a collection of instances. This is done using the Container.Collection.Append overloads:

container.Collection.Append<ILogWriter>(instanceCreator, lifestyle);

这篇关于SimpleInjector:向InstanceCreator注册集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆