InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

How To: Inject 'cross-cutting' decorator to only a subset of implementations

Open patb23 opened this issue 5 years ago • 1 comments

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?

patb23 avatar Aug 03 '20 11:08 patb23

if I add onActivation for Portfolio in my example above, it works - but wanted to specify at a common base class.

patb23 avatar Aug 03 '20 14:08 patb23