[英]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.