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

Avoid using the Func arguments for recursive service first found outside the Func #514

Open
dadhi opened this issue Aug 3, 2022 · 0 comments

Comments

@dadhi
Copy link
Owner

dadhi commented Aug 3, 2022

Example (from https://github.com/Yeah69/DryIoc.Playground/blob/main/DryIoc.Playground/Case3.cs):

namespace DryIoc.Playground;

public static class Case3
{
    public static void Do()
    {
        var container = new Container();

        container.RegisterInstance(0);
        container.Register<DependencyA>();
        container.Register<DependencyB>();
        container.Register<Parent>();

        var parent = container.Resolve<Parent>();
        
        Console.WriteLine(parent.Dependency.Value); // 0
        Console.WriteLine(parent.Dependency.Dependency.Value); // 23
        Console.WriteLine(parent.Dependency.Dependency.Parent.Dependency.Value); // 23 --> BUT EXPECTING 0 
    }

    internal class DependencyA
    {
        public int Value { get; }
        public DependencyB Dependency { get; }

        public DependencyA(
            int value,
            Func<int, DependencyB> dependencyB)
        {
            Value = value;
            Dependency = dependencyB(23);
        }
    }


    internal class DependencyB
    {
        private readonly Lazy<Parent> _parentFactory;
        public int Value { get; }
        public Parent Parent => _parentFactory.Value;

        public DependencyB(
            int value,
            Lazy<Parent> parentFactory)
        {
            _parentFactory = parentFactory;
            Value = value;
        }
    }

    internal class Parent
    {
        public DependencyA Dependency { get; }
    
        public Parent(
            DependencyA dependencyA) =>
            Dependency = dependencyA;
    }
}
dadhi added a commit that referenced this issue Aug 6, 2022
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