Skip to content

Restore and inSingletonScope behavior #715

@rostislavprovodenko

Description

@rostislavprovodenko

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions