views:

86

answers:

1

Hi,

One of my XPCOM components make use of other XPCOM components. 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. Can I pass in bar via foo's constructor? I tried the above but it didn't work.

Zan

A: 

Is it possible to pass in references of other components into your constructor?

No. The parameter to createInstance is an interface; .createInstance(interface) is a shortcut for .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.

Nickolay