简体   繁体   English

简单的注入器如何将批处理注册的泛型类型注入构造函数

[英]simple injector how to inject batch registered generic type into constructor

Simple Injector newbie. 简单的喷油器新手。

I've reviewed this documentation https://simpleinjector.readthedocs.io/en/latest/advanced.html to learn about how to work with generic types with simple injector. 我已经阅读了该文档https://simpleinjector.readthedocs.io/en/latest/advanced.html,以了解如何使用简单的注入器处理泛型类型。

I can't find in the documentation how to inject a class based on a generic type batch registration. 我在文档中找不到如何基于泛型类型批处理注册来注入类。

Here the documentation gives this example: 这里的文档给出了这个例子:

container.Register<IValidator<Customer>, CustomerValidator>();
container.Register<IValidator<Employee>, EmployeeValidator>();
container.Register<IValidator<Order>, OrderValidator>();
container.Register<IValidator<Product>, ProductValidator>();

can be registered like 可以像这样注册

container.Register(typeof(IValidator<>), typeof(IValidator<>).Assembly);

But what datatype do you use in the constructor so that simple injector injects the right concrete datatype. 但是您在构造函数中使用什么数据类型,以便简单的注入器注入正确的具体数据类型。

For example 例如

public classaconstructor(IValidator<Order> vdator)
{
}

I was trying to find the right getinstance method from the documentation, but could not find it. 我试图从文档中找到正确的getinstance方法,但找不到它。 Thx. 谢谢。

The batch registration API you're referring to, is just a function which scans the assembly for all closed implementations of IValidator<T> . 您所引用的批处理注册API只是一个功能,它会扫描程序集以查找IValidator<T>所有封闭实现。

So assuming IValidator<> actually lives in the same assembly as OrderValidator , this call 因此,假设IValidator<>实际上与OrderValidator位于同一程序OrderValidator ,则此调用

container.Register(typeof(IValidator<>), typeof(IValidator<>).Assembly);

will result in at least creating this registration: 将至少导致创建此注册:

container.Register<IValidator<Order>, OrderValidator>();

Which means that this simple unittest should pass: 这意味着此简单的单元测试应通过:

[TestMethod]
public void RegisterIValidator_GetOrderValidatorInstanceSucceeds()
{
    var container = new Container();

    container.Register(typeof(IValidator<>),  typeof(IValidator<>).Assembly);

    container.Verify();

    var orderValidator = container.GetInstance<IValidator<Order>>();

    Assert.IsInstanceOfType(orderValidator, typeof(OrderValidator));
}

Now when you want to use OrderValidator in some other class, you indeed ask for IValidator<Order> in the constructor as you did. 现在,当您想在其他类中使用OrderValidator时,确实确实IValidator<Order>在构造函数中一样要求IValidator<Order> That is the whole point of dependency injection and programming to abstractions. 那就是依赖注入和抽象编程的全部要点。

Simple Injector will never silently fail or inject null . Simple Injector绝不会静默失败或注入null So if injections fails, there will be descriptive exception (with possible inner exceptions) telling you where to look. 因此,如果注入失败,将会有描述性异常(可能还有内部异常)告诉您在哪里查看。

My best guess, with the information provided, is that OrderValidator is living in another assembly. 根据提供的信息,我最好的猜测是OrderValidator正在另一个程序OrderValidator Notice that this overload does accept multiple assemblies. 请注意,此重载确实接受多个程序集。

If this is not the case, post the stacktrace, with inner exceptions as well. 如果不是这种情况,请发布堆栈跟踪,以及内部异常。

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

相关问题 使用Simple Injector注入MVC​​ Controller Constructor时未注册参数 - Parameter not registered when using Simple Injector to inject into an MVC Controller Constructor 简单注入器-在运行时基于指定的泛型类型注入服务 - Simple Injector - Inject service based on the specified generic type at runtime 使用 Simple Injector 基于泛型类型将 DbContext 注入存储库 - Inject DbContext to repository based on the generic type using Simple Injector 如何注入简单的注射器 - How to inject with simple injector 使用Simple Injector注入Web API Controller构造函数时未注册参数 - Parameter not registered when using Simple Injector to inject into an web api Controller Constructor Simple Injector:使用构造函数参数注册开放通用类型 - Simple Injector: Registering open generic type with constructor parameter 为通用接口配置装饰器,并使用Simple Injector中的非通用接口参数将所有实例注入构造函数 - Configure decorators for generic interfaces and inject all instances to constructor with non generic interface argument in Simple Injector 简单注入器-将url值注入服务构造函数 - Simple Injector - Inject url value into service constructor 简单注入器-如何注入类型为Func的属性<T> - Simple Injector - How to inject a property with type Func<T> 检查简单进样器是否已注册类型 - Check whether or not Simple injector has registered type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM