ToInterface

It ensures that the binded object is stored in interface type. In this way, the implementation can be abstracted and dependency on the injected classes is minimized. Whichever implementation of the interface is bound, that implementation is injected.

Sample uses:

public class AppInstaller : GameObjectInstaller
{
    public override void Install(DIContainer container)
    {
        container.Bind<StaticConfigurationService>().ToInterface<IConfigurationService>();
    }
}
public class Foo : MonoBehaviour
{
    [Inject]
    private readonly IConfigurationService _configurationService;
    //StaticConfigurationService object Injected
}
public class AppInstaller : GameObjectInstaller
{
    public override void Install(DIContainer container)
    {
        container.Bind<StaticConfigurationService>().ToInterface<IConfigurationService>();
        container.Bind<RemoteConfigurationService>().ToInterface<IConfigurationService>();
        container.Bind<Foo>();
    }
}
public class Foo
{
    private readonly IConfigurationService[] _configurationServices;
    //StaticConfigurationService and RemoteConfigurationService objects Injected

    public Foo(IConfigurationService[] configurationServices)
    {
       _configurationServices = configurationServices;
    }
}

Last updated