Zenject icon indicating copy to clipboard operation
Zenject copied to clipboard

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

Open leonidumanskiy opened this issue 3 years ago • 0 comments

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.

leonidumanskiy avatar Dec 02 '22 07:12 leonidumanskiy