Alternatives to OpaqueToken for use in dependency-injection (DI) chapter #1563
Description
OpaqueToken cannot be implemented in Dart, see angulardart/angular#35. I am opening this issue to hold a thread of discussion about alternatives for both the Dart and TS sides --- given that we want minimize difference across languages so as to provide a consistent UX. I'll focus the discussion on Dart, but the proposal holds for both.
For the purpose of this discussion, let's reuse the example given in the TS OpaqueToken section of the angular.io di chapter. Consider the following snippets of a Dart version of that example:
const APP_CONFIG = const OpaqueToken('app.config'); // Token definition
// ... providers
providers: const [const Provider(APP_CONFIG, useValue: CONFIG)]
// ... constructor
AppComponent(@Inject(APP_CONFIG) Config config) {...}
Given that an OpaqueToken
is no better than a String
in Dart, why not simply use an fresh type name introduced via an abstract class declaration:
abstract class APP_CONFIG {} // Token definition
Given that TS also has abstract classes, this approach could be used consistently across languages.