I've been reading the Angular 2.0 docs about dependency inversion and I've been looking also at some online examples.
My understanding is that the @injectable
decorator uses the TypeScript compiler with the emitDecoratorMetadata
flag to create metadata that is used to resolve dependencies. Take for example the following class:
The TypeScript compiler uses emitDecoratorMetadata
to declare via metadata that the DataService
class has a constructor argument with type Http
.
Once we have declared the dependencies of a class using @Injectable
we can indicate that it needs to be injected into some components using the Provides
option in the @App
or @Component
decorators.
I'm aware of the behaviour of emitDecoratorMetadata
and I know that it cannot emit metadata for interfaces. Therefore, I assume that I cannot depend upon IHttp
instead of Http
:
Is my assumption correct? Can I depend “Depend upon Abstractions. Do not depend upon concretions.” or is that something that is not possible at the moment? I assume this will be fixed as soon as the emitDecoratorMetadata
becomes capable of serialising interfaces.
Currently you need a type, a string name or an OpaqueToken
as key for providers.
Interfaces are not supported because the information is not available at runtime. If this gets added, I'm sure DI will support them (it's already supported in Dart).