modesttree / Zenject

Dependency Injection Framework for Unity3D
MIT License
2.5k stars 273 forks source link

[Zenject 6 Upgrade] Not possible to bind multiple interfaces to the same implementation when using WithId anymore? #277

Open leonidumanskiy opened 1 year ago

leonidumanskiy commented 1 year ago

Consider this example:

interface IAnimal { ... }

interface ICanine { ... }
interface IFeline { ... }

class Dog : IAnimal, ICanine { ... }
class Cat : IAnimal, IFeline { ... }

Container.Bind<ICanine>().To<Dog>().AsSingle();
Container.Bind<IFeline>().To<Cat>().AsSingle();

Container.Bind<IAnimal>().WithId("dog").To<Dog>().AsSingle();
Container.Bind<IAnimal>().WithId("cat").To<Cat>().AsSingle();

Before the upgrade in the version 5.5, this worked and you could resolve a Dog using both traditional injection into e.g. a constructor (ICanine), as well as via something like an AnimalFactory when you have an ID.

Now with the latest Zenject, I am getting:

ZenjectException: Assert hit! Attempted to use AsSingle multiple times for type 'Dog'.  As of Zenject 6+, AsSingle as can no longer be used for the same type across different bindings.  See the upgrade guide for details.

I am unsure how to resolve it since you can't use WithId() on a single binding where first interface needs to resolve without an ID.