1

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

Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
liangzan
  • 6,804
  • 4
  • 29
  • 28

1 Answers1

1

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
  • 31,095
  • 13
  • 107
  • 185