[英]Dependency Injection in XPCOM
One of my XPCOM components make use of other XPCOM components. 我的XPCOM组件之一利用了其他XPCOM组件。 As I was testing it, I found it cumbersome cos of the dependencies.
在测试时,我发现它的依赖项繁琐。 Then I thought of using Dependency Injection to pass in the other components in my constructor.
然后,我想到了使用依赖注入在构造函数中传入其他组件。 I wasn't successful.
我没有成功。 Is it possible to pass in references of other components into your constructor?
是否可以将其他组件的引用传递到构造函数中?
var _foo = Components.classes["@foo.com/foo;1"].createInstance(bar);
this.foo = _foo.QueryInterface(Components.interfaces.IFoo);
For example, foo component needs bar. 例如,foo组件需要bar。 Can I pass in bar via foo's constructor?
我可以通过foo的构造函数传入bar吗? I tried the above but it didn't work.
我尝试了上面的方法,但是没有用。
Zan 赞
Is it possible to pass in references of other components into your constructor?
是否可以将其他组件的引用传递到构造函数中?
No. The parameter to createInstance
is an interface; 否
createInstance
的参数是一个接口; .createInstance(interface)
is a shortcut for .createInstance().QueryInterface(interface)
. .createInstance(interface)
是.createInstance().QueryInterface(interface)
的快捷方式。
The answer to your problem lies in the area you didn't describe in the question - most likely you don't need to access the other component from your "constructor" or you stumbled on a problem you could fix, but instead "cumbersome cos of the dependencies" and moved on. 问题的答案在于您未在问题中描述的区域-最有可能您不需要从“构造函数”访问其他组件,或者偶然发现了可以解决的问题,而是“麻烦的cos依赖项”,然后继续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.