假设我有以下类来实现以下接口:
class MyClassA : IInterface<MyClassA>, IInterface
class MyClassB : IInterface<MyClassB>, IInterface
我还有另外两个类,如下所示:
class ImportA {
public ImportA(IEnumerable<IInterface>) {}
}
class ImportB {
public ImportB(IInterface<MyClassA>) {}
}
IInterface
的类注入ImportA。IInterface<MyClassA>
。我无法弄清楚如何使用Simple Injector。我已尝试RegisterCollection
和https://simpleinjector.readthedocs.org/en/latest/howto.html#register-multiple-interfaces-with-the-same-implementation
我最接近的是得到一个错误,说一个类作为Singleton导入,另一个作为Transient导入。
我尝试将所有LifeStyles更改为Singleton并默认将配置设置为Singleton,但无济于事。
更新1: 我正在寻找一种自动化的方法。我有很多课程需要注册,并且不想手动完成。这是一个不同的样本。以下注册示例不起作用。自动化失败了:
An exception of type 'SimpleInjector.ActivationException' occurred in
SimpleInjector.dll but was not handled in user code
Additional information: The constructor of type MyClassB contains the
parameter with name 'myClassA' and type IInterface<MyClassA> that is not
registered. Please ensure IInterface<MyClassA> is registered, or change the
constructor of MyClassB. There is, however, a registration for
IEnumerable<IInterface<MyClassA>>; Did you mean to depend on
IEnumerable<IInterface<MyClassA>>?
这是我目前的代码:
public interface IInterface {
string Value { get; set; }
}
public interface IInterface<T> : IInterface { }
public class MyClassA : IInterface<MyClassA> {
public string Value { get; set; }
}
public class MyClassB : IInterface<MyClassB> {
private IInterface<MyClassA> myClassA;
public MyClassB(IInterface<MyClassA> myClassA) {
this.myClassA = myClassA;
}
public string Value {
get { return this.myClassA.Value; }
set { this.myClassA.Value = value; }
}
}
public class ImportA {
public ImportA(IEnumerable<IInterface> imports) {
// fails on this line
var a = imports.SingleOrDefault(i => i is IInterface<MyClassA>);
a.Value = "test";
var b = imports.SingleOrDefault(i => i is IInterface<MyClassB>);
// should output the value of "test";
Console.WriteLine(b.Value);
Console.ReadKey();
}
}
...
static void Main() {
var container = new Container();
var types = container.GetTypesToRegister(
typeof(IInterface<>), new[] { typeof(IInterface<>).Assembly });
var registrations = (
from type in types
select Lifestyle.Singleton.CreateRegistration(type, type, container))
.ToArray();
container.RegisterCollection<IInterface>(registrations);
container.RegisterCollection(typeof(IInterface<>), registrations);
var a = container.GetInstance(typeof(ImportA));
}
ImportA 是入口点,应该是Transient。 其他所有内容都应该是单例,以便在ImportA中修改MyClassA时,可以在MyClassB中获取该值。
答案 0 :(得分:5)
执行此操作的方法是手动创建Registration
个实例,并使用它们注册两个单独的集合。示例:
var a = Lifestyle.Singleton.CreateRegistration<MyClassA>(container);
var b = Lifestyle.Singleton.CreateRegistration<MyClassB>(container);
container.RegisterCollection<IInterface>(new[] { a, b });
container.AddRegistration(typeof(IInterface<MyClassA>), a);
container.AddRegistration(typeof(IInterface<MyClassB>), b);
这将确保在两个集合中使用相同的单个实例。如果一个类实现了IInterface<T>
的多个通用版本,那么同一个单个实例甚至可以在IInterface<T>
的不同集合上重用。
如果您想要更自动化的方法,可以这样做:
var types = container.GetTypesToRegister(typeof(IInterface<>), assemblies);
// NOTE: The ToArray() call is crucial to prevent torn lifestyles.
var registrations = (
from type in types
select Lifestyle.Singleton.CreateRegistration(type, type, container))
.ToArray();
container.RegisterCollection<IInterface>(registrations);
foreach (var registration in registrations) {
container.AddRegistration(
typeof(IInterface<>).MakeGenericType(registration.ImplementationType),
registration);
}