SimpleInjector : Register collection with InstanceCreator

主宰稳场 提交于 2021-01-28 08:14:33

问题


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.

Basically what i'm trying to do is:

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

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


回答1:


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.

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);


来源:https://stackoverflow.com/questions/60781095/simpleinjector-register-collection-with-instancecreator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!