简体   繁体   English

向Simple Injector注册收集

[英]Register collection with Simple Injector

I try to register a collection with simple injector in c#. 我尝试在C#中使用简单的注入器注册一个集合。 My approach was the following: 我的方法如下:

container.Collection.Register<IValidateMitarbeiter>(
    typeof(MitarbeiterVerfuegbarkeitValidator),
    typeof(MitarbeiterQualifikationsValidator));

But i get this error message: 但我收到此错误消息:

The configuration is invalid. 配置无效。 Creating the instance for type IDispoLinienManager failed. 为IDispoLinienManager类型创建实例失败。 The constructor of type DispoLinienManager contains the parameter with name 'mitarbeiterValidators' and type IValidateMitarbeiter that is not registered. 类型DispoLinienManager的构造函数包含名称为'mitarbeiterValidators'的参数和未注册的IValidateMitarbeiter类型。 Please ensure IValidateMitarbeiter is registered, or change the constructor of DispoLinienManager. 请确保IValidateMitarbeiter已注册,或更改DispoLinienManager的构造函数。

This is the constructor of DispoLinienManager 这是DispoLinienManager的构造函数

public DispoLinienManager(IDataContextFactory dataContextFactory,
        IDispoPlanLinieFactory dispoPlanLinieFactory,
        IValidateMitarbeiter mitarbeiterValidators)
    {
        this.dataContextFactory = dataContextFactory;
        this.dispoPlanLinieFactory = dispoPlanLinieFactory;
        this.mitarbeiterValidators = mitarbeiterValidators;
    }

IValidateMitarbeiter has two implementations who are both used in DispoLinienManager. IValidateMitarbeiter具有两个在DispoLinienManager中使用的实现。 If there are any missing informations, i will be glad to help you to solve my problem. 如果有任何遗漏的信息,我将很高兴帮助您解决我的问题。

If I reproduce your example locally, using the latest version of Simple Injector, I get the following message: 如果我使用最新版本的Simple Injector在本地复制您的示例,则会收到以下消息:

The configuration is invalid. 配置无效。 Creating the instance for type DispoLinienManager failed. 创建类型DispoLinienManager的实例失败。 The constructor of type DispoLinienManager contains the parameter with name 'mitarbeiterValidators' and type IValidateMitarbeiter that is not registered. 类型DispoLinienManager的构造函数包含名称为'mitarbeiterValidators'的参数和未注册的IValidateMitarbeiter类型。 Please ensure IValidateMitarbeiter is registered, or change the constructor of DispoLinienManager. 请确保IValidateMitarbeiter已注册,或更改DispoLinienManager的构造函数。 There is, however, a registration for IEnumerable<IValidateMitarbeiter>; 但是,存在IEnumerable <IValidateMitarbeiter>的注册; Did you mean to depend on IEnumerable<IValidateMitarbeiter> ? 您是要依赖IEnumerable <IValidateMitarbeiter>吗?

Do note the last part of that message. 请注意该消息的最后一部分。

As described here in the documentation, Simple Injector separates registration of collections from one-to-one mappings: 如文档中此处所述,Simple Injector将集合的注册与一对一映射分开:

When designing Simple Injector, we made a very explicit design decision to define a separate RegisterCollection method for registering a collection of services for an abstraction. 在设计Simple Injector时,我们做出了非常明确的设计决策,以定义单独的RegisterCollection方法来注册抽象服务的集合。

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

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