Ways of consuming extensions in a class

What is the difference between consuming an extension as paramter in the constructor and directly as a field in the class.

For the reference below is the code snap:

@injectable()
export class SimpleConsumer {
   @inject(InterfaceA) private interfaceA: InterfaceA;

   constructor(
       @inject(InterfaceB) private interfaceB: InterfaceB
   ) { }

   doSth() {
       this.interfaceA.foo();
       this.interfaceB.bar();
   }
}

Here InterfaceA and InterfaceB are two extensions which are consumed differently, can’t they be consumed in only constructor or as class fields?

1 Like

@Abhay0294 There is no real functional difference between those styles. Theia just uses the constructor injection approach in older classes, which were written before inversify supported property injection if I remember correctly.

@Abhay0294 for additional information you can take also take a look at the documentation from Inversify.JS:

In your example you won’t be able to use this.interfaceA in your constructor, instead you’d need to use Inversify’s postConstruct to tag a method to be called in order to initialize your instance after it has been constructed and after the properties were injected by Inversify.

Depending on what you are trying to do you may prefer one style over the other, one isn’t always better than the other.

1 Like