-
-
Notifications
You must be signed in to change notification settings - Fork 718
Restore and inSingletonScope behavior #715
Description
Expected Behavior
Injecting a singleton value after container.restore() reinitializes it.
Current Behavior
Singleton values should be cached and not initialized twice.
Steps to Reproduce (for bugs)
var inversify = require("inversify");
require("reflect-metadata");
const container = new inversify.Container();
container.bind('bla').toDynamicValue(() => {
console.log('------initializing------');
return 'bla';
}).inSingletonScope();
container.get('bla');
container.get('bla');
console.log('---------snapshot and restore---');
container.snapshot();
container.restore();
container.get('bla');
container.get('bla');
Gives the following output:
------initializing------
---------snapshot and restore---
------initializing------
Whereas I would expect to get the following result:
------initializing------
---------snapshot and restore--
Context
We have some singletons that require additional initialization outside the toDynamicValue function (to avoid circular dependencies). After the restore method is called and a singleton is injected again the additional initialization should be run again, which is undesirable. I looked at the inversify code and it seems that singleton values should be preserved across restores, but they are not.