Skip to content

Null passed as param when registration injects itself #133

@0xivaan

Description

@0xivaan

Describe the bug
During our migration from Unity to Pure.DI we stumbled upon a wrongly written registration which caused the generated code to pass null as argument.

To Reproduce

using Pure.DI;

new Composition().Resolve<Func<IApp>>();

interface IApp { }
interface IConfigProvider { }
class ConfigProvider : IConfigProvider
{
    public ConfigProvider(Func<IApp> appFactory)
    {
        // PROBLEM: appFactory is null
    }
}

class App(IConfigProvider configProvider) : IApp
{
}

public partial class Composition
{
    public void Setup()
    {
        var binding = DI.Setup(nameof(Composition))
        .Bind<Func<IApp>>().As(Lifetime.Singleton).To<Func<IApp>>(ctx =>
        {
            ctx.Inject<IApp>(out var appMode);
            return () => appMode;
        }).Root<Func<IApp>>()
        .Bind<IConfigProvider>().As(Lifetime.Singleton).To(ctx =>
        {
            ctx.Inject(out Func<IApp> winBackStatusFactory);
            return new ConfigProvider(winBackStatusFactory);
        })
        .Bind<IApp>().As(Lifetime.Singleton).To(ctx =>
        {
            ctx.Inject<IConfigProvider>(out var tagManagerSetup);
            return new App(tagManagerSetup);
        })
        ;
    }
}

The problem is in the Func registration. The correct way to register it is this:

.Bind<Func<IApp>>().As(Lifetime.Singleton).To<Func<IApp>>(ctx => () =>
        {
            ctx.Inject<IApp>(out var appMode);
            return appMode;
        }).Root<Func<IApp>>()

Expected behavior
I'm not really sure, perhaps compile-time error?

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions