My question is: can i rebind an instance and re-inject it everywhere i have it injected with [Inject] attribute. Instance was bound like this:
[SerializeField] private MyClass inst;
public override InstallBindings()
{
Container.BindInstance(inst);
}
And then, when inst got changed, i tried to do:
Container.BindInstance(inst);
Container.QueueForInject(inst);
but it didn't work, and every field with type MyClass that was injected like this [Inject] private MyClass inst;
the same as before. Mb i'm just misunderstanding the concept. Is it possible at all?
You can use rebind like this:
Container.Rebind<Foo>().FromInstance(foo)
Or:
Container.Unbind<Foo>();
Container.BindInstance(foo);
However, this kind of thing is considered bad practice. When using dependency injection properly, the bindings will only be added once at install time and will not need to change again. This is important because objects might already have been created that need access to the changes made later to the container.
If the instance of your class changes at runtime, then another way to do it might be to bind a class that wraps the active instance and then fires events when it changes.