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

Resolving func-wrapper when having circular-dependency throws RecursiveDependencyDetected #651

Open
BinaryCraX opened this issue Jul 2, 2024 · 1 comment
Assignees
Milestone

Comments

@BinaryCraX
Copy link

Resolving a service with a circular dependency from the container crashes with ContainerException (Error.RecursiveDependencyDetected) even though a func-wrapper is used.
However the same code works when using a lazy-wrapper.

The documentation states that both should work (even though there is no explicit example for the func-wrapper).

Following code crashes:

using DryIoc;

namespace DryIocCircularDep
{
    public class Child
    {
        public Child(Parent parent)
        {
            Parent = parent;
        }

        public Parent Parent { get; }
    }

    public class Parent
    {
        public Parent(Func<Child> child)
        {
            Child = child;
        }

        public Func<Child> Child { get; }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            var container = new Container();
            container.Register<Child>();
            container.Register<Parent>(Reuse.Singleton);

            var parent = container.Resolve<Parent>(); // <-- crashes

            Console.WriteLine(parent.Child().Parent == parent);
        }
    }
}

As a reference, working code using lazy-wrapper:

using DryIoc;

namespace DryIocCircularDep
{
    public class Child
    {
        public Child(Parent parent)
        {
            Parent = parent;
        }

        public Parent Parent { get; }
    }

    public class Parent
    {
        public Parent(Lazy<Child> child)
        {
            Child = child;
        }

        public Lazy<Child> Child { get; }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            var container = new Container();
            container.Register<Child>();
            container.Register<Parent>(Reuse.Singleton);

            var parent = container.Resolve<Parent>(); // <-- OK

            Console.WriteLine(parent.Child.Value.Parent == parent);
        }
    }
}

DryIoc-Version: 5.4.3

@dadhi
Copy link
Owner

dadhi commented Jul 2, 2024

@BinaryCraX
Hi, I am afraid that there is an error in this part of the documentation.
Here is the correct one https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/Wrappers.md#func-of-a
You may submit a PR or I will fix it myself.

@dadhi dadhi self-assigned this Aug 29, 2024
@dadhi dadhi added this to the v6.0.0 milestone Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants