InversifyJS
InversifyJS copied to clipboard
How To: Inject 'cross-cutting' decorator to only a subset of implementations
Base Class:
@injectable()
export abstract class MWServiceClient implements ServiceClient{
introduced another abstract base class for adding 'caching' decorator:
@fluentProvide(TYPES.MWServiceClient).onActivation(((context: Context) => {
console.log(`erl ${context} `); // want to introduce caching decorator
})).done()
// @injectable()
export abstract class CacheableServiceClient extends MWServiceClient implements ServiceClient {
In implementations where I want to cache, I am extending them to the 'Cacheable' base class.
@fluentProvide(TYPES.MWServiceClient).whenTargetTagged("party", true).done()
export class Portfolio extends CacheableServiceClient implements ServiceClient{
constructor() {
super(configOption);
}
When I run the app, I notice that the constructor is called twice - one with 'configOption' and another with null/empty configOption. If I replace the 'fluentProvider' in CacheableServiceClient with 'injectable', the app starts up fine.
Could I get suggestions/references on how to selectivley apply cross-cutting decorators?
if I add onActivation for Portfolio in my example above, it works - but wanted to specify at a common base class.