inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.03k stars 710 forks source link

Global activation hooks #471

Open captainjono opened 7 years ago

captainjono commented 7 years ago

Lets say i have a series of modules defined for an app. Each module adds certain features to the app. I know that modules will provide services that implement certain interfaces.

At runtime, i want to know when any of these services are first resolved from the container, either directly, or indirectly via a child dependency resolution.

Currently with middleware, i can only see a way to get access to an instance of the directly requested service. The only way i can get access to the instance of the child service is to use the OnActivation method thats created during the binding.

This is a cross cutting concern, i cant/or want to do this at registration time. It would be great if this could be supported. Currently im using a forked version of the repo to meet this requirement.

remojansen commented 7 years ago

👍 thanks for this suggestion. Sorry for the late reply I was on holidays... I will try to do something about this and share my plans here soon...

fyodorvi commented 7 years ago

I'm using this:

inversifyContainer.bind = <any>_.wrap(inversifyContainer.bind, (originalBind, ...rest: any[]) => {
    const binding = originalBind.apply(inversifyContainer, rest);
    binding._binding.onActivation = (context, instance) => {
        wireInstanceConfig(instance);
        return instance;
    };
    return binding;
});

Terribly dirty, I know, but that's the only way for now.

Moln commented 5 years ago

I need this feature, too.

tonyhallett commented 4 years ago

I will create a pull request for this tomorrow.