Extenject icon indicating copy to clipboard operation
Extenject copied to clipboard

Bind<Foo>().FromFactory<Foo.Factory>() Results in "zenject exception: cannot resolve IProvider when building type Foo.Factory."

Open Monsoonexe opened this issue 5 months ago • 0 comments

Describe the bug I'm trying to use the bindings:

Container.Bind<Foo>().FromFactory<Foo.Factory>().AsSingle() and Container.BindFactory<Foo, Foo.Factory>();

However, this results in "Zenject.ZenjectException: Unable to resolve 'IProvider' while building object with type 'Foo.Factory'. Object graph: Foo.Factory".

To Reproduce

public class Foo
{
    public class Factory : PlaceholderFactory<Foo> {}
}

public class MyInstaller  : Installer
{
  Container.Bind<Foo>().FromFactory<Foo.Factory>().AsSingle();
  Container.BindFactory<Foo, Foo.Factory>();
}

// container.Resolve<Foo>(); should work because zenject should be configured to create it by using the factory with a cached provider.

Expected behavior I expect that Zenject would figure out which provider to use based on the AsCached or FromNew or something. Or I would expect the Finalizer to throw an exception during the binding step if I were doing something wrong (like an "error on previous binding" message). I think IProvider is internal to Zenject and I probably shouldn't try to fill this dependency myself.

Workaround I can change Container.Bind<Foo>().FromFactory<Foo.Factory>() to Container.Bind<Foo>().FromMethod((ctx) => ctx.Container.Resolve<Foo.Factory>().Create() and it works as expected. I would think these would be functionally equivalent.

Am I doing something wrong to get this exception? Should I be doing the workaround instead of binding directly to a factory? The docs make it seem like this is the way to accomplish what I want.

  • Zenject version: 9

Monsoonexe avatar Aug 27 '24 16:08 Monsoonexe