ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 363 forks source link

Using interface like return value in Factory #640

Closed ManHunterGroms closed 5 years ago

ManHunterGroms commented 5 years ago

In example code below give error:

ZenjectException: Assert hit! Expected non-abstract type for given binding but instead found type 'IFoo'

Why I can't use interface like return value?

    public interface IMyFooFactory : IFactory<IFoo>
    {
    }

    public interface IFoo
    {

    }

    public class Foo : IFoo
    {
        public class Factory : PlaceholderFactory<IFoo>, IMyFooFactory
        {
        }
    }

    public class FooInstaller : MonoInstaller<FooInstaller>
    {
        public override void InstallBindings()
        {
            Container.BindFactoryCustomInterface<IFoo, Foo.Factory, IMyFooFactory>();
        }
    }

Another expample work fine!

    public interface IMyFooFactory : IFactory<IFoo>
    {
    }

    public interface IFoo
    {

    }

    public class CustomFooFactory : IMyFooFactory
    {
        public IFoo Create()
        {
            return new Foo();
        }
    }

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

    public class FooInstaller : MonoInstaller<FooInstaller>
    {
        public override void InstallBindings()
        {
            Container.BindFactory<IFoo, Foo.Factory>().FromFactory<CustomFooFactory>();
        }
    }

but now I can't use IMyFooFactory for Injection.

ManHunterGroms commented 5 years ago

I finded solution, but not sure is this good. In example №2 replace BindFactory:

Container.BindFactoryCustomInterface<IFoo, Foo.Factory, IMyFooFactory>().FromFactory<CustomFooFactory>();

and implement interface IMyFooFactory in Foo.Factory class:

public class Factory : PlaceholderFactory<IFoo>, IMyFooFactory
{
}