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

+1


a source to share


1 answer


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.

+1


a source







All Articles