有没有办法注册单个接口,该接口由多个具体类使用[simple-injector]并且不使用模板接口实现?
说我们有2节课 MyClass1 和 Myclass2 这两个类都在实现 IInterface1
现在使用[simple-injector]我们无法做到这一点
container.Register<IInterface1, Myclass1>();
container.Register<IInterface1, Myclass2>();
将现有接口转换为模板接口对现有代码库来说是一项艰巨的任务。希望有一些更容易的。
您可以使用以下命令注册同一接口的多个实现 RegisterCollection 方法(见 文件:配置要返回的实例的集合)
所以你需要写:
container.Collection.Register<IInterface1>(typeof(Myclass1), typeof(Myclass2));
而现在Simple Injector可以注入一个集合 Interface1 实现到您的构造函数中,例如:
public class Foo
{
public Foo(IEnumerable<IInterface1> interfaces)
{
//...
}
}
或者您可以明确解决您的问题 IInterface1 实现与 GetAllInstances:
var myClasses = container.GetAllInstances<IInterface1>();
您可以使用以下命令注册同一接口的多个实现 RegisterCollection 方法(见 文件:配置要返回的实例的集合)
所以你需要写:
container.Collection.Register<IInterface1>(typeof(Myclass1), typeof(Myclass2));
而现在Simple Injector可以注入一个集合 Interface1 实现到您的构造函数中,例如:
public class Foo
{
public Foo(IEnumerable<IInterface1> interfaces)
{
//...
}
}
或者您可以明确解决您的问题 IInterface1 实现与 GetAllInstances:
var myClasses = container.GetAllInstances<IInterface1>();