2
使用Robotlegs 2.0,是否可以在上下文中声明一个单例并将其注入多个类中,其中每个注入都请求不同的接口?如何通过不同的接口注入同一个对象?
类注入:
class MyClass implements IFoo, IBar {}
注射部位1:
class NeedFoo {
[Inject]
public var foo:IFoo;
}
注射部位2:
class NeedBar {
[Inject]
public var bar:IBar;
}
两个我都想要打针是相同的实例,但如果我创建两个映射,那么每个映射都有自己的实例:
injector.map(IFoo).toSingleton(MyClass);
injector.map(IBar).toSingleton(MyClass);
所以我落得这样做的:
var instance:MyClass = new MyClass();
injector.map(IFoo).toValue(instance);
injector.map(IBar).toValue(instance);
有没有办法,只是说,这个对象应该为它实现,而不是单独为每一个任何界面来提供?
是的,它可以用一些糖做的..但它做这份工作!谢谢。 –
某个问题跟踪器中是否有票? –
Yeps:https://github.com/robotlegs/robotlegs-framework/issues/142 – Creynders