19

Simple scenario:

I have multiple Services that implement a common Interface. All those Services are registered within the bootstrap method.

Now I'd like to have another Service, which injects all registered Services that implement the common Interface.

i.e.

export interface MyInterface {
    foo(): void;
}

export class Service1 implements MyInterface {
    foo() { console.out("bar"); }
}

export class Service2 implements MyInterface {
    foo() { console.out("baz"); }
}

export class CollectorService {
    constructor(services:MyInterface[]) {
        services.forEach(s => s.foo());
    }
}

Is that possible somehow?

Benjamin M
  • 23,599
  • 32
  • 121
  • 201

2 Answers2

17

You need to register your service providers like this:

boostrap(AppComponent, [
  provide(MyInterface, { useClass: Service1, multi:true });
  provide(MyInterface, { useClass: Service2, multi:true });
]);

This will work only with classes not with interfaces since interfaces don't exist at runtime.

To make it work with interfaces, you need to adapt it:

bootstrap(AppComponent, [
  provide('MyInterface', { useClass: Service1, multi:true }),
  provide('MyInterface', { useClass: Service2, multi:true }),
  CollectorService
]);

and inject this way:

@Injectable()
export class CollectorService {
  constructor(@Inject('MyInterface') services:MyInterface[]) {
    services.forEach(s => s.foo());
  }
}

See this plunkr for more details: https://plnkr.co/edit/HSqOEN?p=preview.

See this link for more details:

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
12

Because interfaces are not available at runtime (only for static checks) interfaces can't be used as toke for DI.

Use a token instead:

(deprecated) https://angular.io/api/core/OpaqueToken

var myInterfaceToken = new OpaqueToken('MyInterface');

https://angular.io/api/core/InjectionToken

var myInterfaceToken = new InjectionToken<MyInterface>('MyInterface');
// import `myInterfaceToken` to make it available in this file

@NgModule({
  providers: [ 
    { provide: myInterfaceToken, useClass: Service1, multi:true },
    { provide: myInterfaceToken, useClass: Service2, multi:true },
  ],
  boostrap: [AppComponent],
)
class AppComponent {}
// import `myInterfaceToken` to make it available in this file

export class CollectorService {
    constructor(@Inject(myInterfaceToken) services:MyInterface[]) {
        services.forEach(s => s.foo());
    }
}
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Using OpaqueToken is the recommended way of providing interface implementations. – Schmuli Jul 12 '16 at 11:13
  • Used `InjectionToken` instead of `OpaqueToken` since the last is deprecated since angular 4. https://angular.io/api/core/InjectionToken – FindOutIslamNow Jun 20 '18 at 10:49
  • 1
    Using a mix of this answer and [Angular2 How can I Inject all interface implementation to service as list?](https://stackoverflow.com/questions/43049643/angular2-how-can-i-inject-all-interface-implementation-to-service-as-list), I managed to make things work. Thanks! To this answer: The provider syntax has changed to something like this: `{ provide: MY_CLASS_INJECTION_TOKEN, useClass: MY_CLASS, multi: true }` and is added to the start module (at least I add it to the start module...) – MartinJH Sep 04 '18 at 07:36
  • 1
    @MartinJH glad to hear! Thanks for the hint. It's quite an old answer. I updated it. – Günter Zöchbauer Sep 04 '18 at 07:44
  • 1
    That was a speedy response. Thanks for the effort! – MartinJH Sep 04 '18 at 07:45