使用 Simple Injector 引导混凝土列表
Bootstrapping a list of concretes using Simple Injector
我有一个 class 接受多种类型的接口,所有接口都继承自接口 IVehicle
。我正在为 IOC 使用 Simple Injector。我目前的 class 看起来像这样。
public class MyClass
{
public MyClass(ICar car, IMotorcycle motorcycle)
{
}
}
但是,我试图摆脱传递每种类型的 IVehicle 并只传递一个 IVehicle 列表。包含 ICar 和 IMotorcycle 接口的列表。所以我想要的 class 看起来像这样。
public class MyClass
{
public MyClass(IEnumerable<IVehicle> vehicles)
{
}
}
我对 class 进行了修改,但我不确定如何 bootstrap 以使其传递所有 IVehicles 类型。
我尝试在 bootstrapping 中做类似的事情。
<IEnumerable<IVehicle>, List<IVehicle>>
但是,在我的 class 中,当我查看我的列表时它是空的。有谁知道我如何才能使该列表填充我的 IVehicle
类型(ICar、IMotorcycle)?
Simple Injector contains several methods for registering and resolving collections of types. Here are some examples:
// Configuration
// Registering a list of instances that will be created by the container.
// Supplying a collection of types is the preferred way of registering collections.
container.RegisterCollection<ILogger>(new[] { typeof(MailLogger), typeof(SqlLogger) });
[...]
// Usage
var loggers = container.GetAllInstances<ILogger>();
翻译成你的情况意味着:
container.RegisterCollection<IVehicle>(new[] { typeof(Car), typeof(Motorcycle) });
我有一个 class 接受多种类型的接口,所有接口都继承自接口 IVehicle
。我正在为 IOC 使用 Simple Injector。我目前的 class 看起来像这样。
public class MyClass
{
public MyClass(ICar car, IMotorcycle motorcycle)
{
}
}
但是,我试图摆脱传递每种类型的 IVehicle 并只传递一个 IVehicle 列表。包含 ICar 和 IMotorcycle 接口的列表。所以我想要的 class 看起来像这样。
public class MyClass
{
public MyClass(IEnumerable<IVehicle> vehicles)
{
}
}
我对 class 进行了修改,但我不确定如何 bootstrap 以使其传递所有 IVehicles 类型。
我尝试在 bootstrapping 中做类似的事情。
<IEnumerable<IVehicle>, List<IVehicle>>
但是,在我的 class 中,当我查看我的列表时它是空的。有谁知道我如何才能使该列表填充我的 IVehicle
类型(ICar、IMotorcycle)?
Simple Injector contains several methods for registering and resolving collections of types. Here are some examples:
// Configuration // Registering a list of instances that will be created by the container. // Supplying a collection of types is the preferred way of registering collections. container.RegisterCollection<ILogger>(new[] { typeof(MailLogger), typeof(SqlLogger) }); [...] // Usage var loggers = container.GetAllInstances<ILogger>();
翻译成你的情况意味着:
container.RegisterCollection<IVehicle>(new[] { typeof(Car), typeof(Motorcycle) });