Open 1b0x opened 2 years ago
A better approach is to create a child container in the factory. This way, the original container isn't modified:
export function SceneFactory(context: interfaces.Context) {
return (key: string, scene: any) => {
const childContainer = context.container.createChild();
// Bind the new scene class (since this is bound in the child container, it will override any existing binds)
childContainer.bind<IScene>(key).to(scene);
return childContainer.get<IScene>(key);
};
}
More info here: https://github.com/inversify/InversifyJS/blob/master/wiki/hierarchical_di.md
Thank you! I really appreciate it!
Is it good to create multiple child containers for other manager classes? Let's say that If I have Sound Manager (which has SoundFactory), should I create a new child container?
Hello there, I have a question about this piece of code:
This is how my factory looks like. As you can see I'm binding/unbinding some classes inside this factory method.
I'm using it like that:
So, the question is: Is it okay to bind/unbind classes in a factory method? Is it a good approach to do that?