Dependency Injection in XPCOM
One of my XPCOM components uses other XPCOM components. When I tested it, I found it cumbersome compared to dependencies. Then I thought about using Dependency Injection to pass in other components of my constructor. I did not succeed. Is it possible to pass references to other components to your constructor?
var _foo = Components.classes["@foo.com/foo;1"].createInstance(bar);
this.foo = _foo.QueryInterface(Components.interfaces.IFoo);
For example, the needs panel foo. Can I step through the bar through the foo constructor? I tried above but it didn't work.
Zan
a source to share
Is it possible to pass references to other components to your constructor?
No. The parameter createInstance
is an interface; .createInstance(interface)
is a shortcut for .createInstance().QueryInterface(interface)
.
The answer to your problem lies in an area that you did not describe in the question - most likely, you will not need to access another component from your "constructor", or you stumbled upon a problem that you can fix, but instead a "bulky cos of dependencies" and moved on.
a source to share