Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Monsoonexe opened this issue Aug 27, 2024 · 0 comments

Comments

@Monsoonexe
Copy link

Monsoonexe commented Aug 27, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant