DryIoc 递归依赖异常与工厂 (Func<>)

DryIoc recursive dependency exception with Factory (Func<>)

嘿,我已经从 Autofac 切换到 DryIoc。我的代码以前有效,但现在导致异常“解析时检测到递归依赖性”。 (代码已简化)

public class AFactory {
   public AFactory(Func<A> getA){ }
}

public class BFactory {
   public BFactory(Func<B> getB, AFactory factory){ }
}

public class A { 
   public A(IrrelevantService service, BFactory factory){ }
}

实际代码很复杂,因此假设有必要使用此代码结构的原因。

它正在尝试解析 AFactory --> A --> BFactory --> AFactory,这是导致问题的原因。但是因为它使用的是 Func<> 所以应该没问题吧? (或者至少它在 Autofac 中)。

有没有办法注册它,使其不抛出异常?

这里是 reason from the Func wrapper docs:

By default, does not permit recursive dependency.

下面的文档或 later section.

中描述了修复

Here are all possible fixes:

using System;
using DryIoc;
                    
public class Program
{
    public static void Main()
    {
        var c = new Container(
            //rules => rules.WithFuncAndLazyWithoutRegistration() // fix1: make everything lazy resolvable preventing the recursive dependency check!
        );
        
        c.Register<A>(
            //setup: Setup.With(asResolutionCall: true) // fix2: makes A a dynamically resolvable
        );
        c.Register<AFactory>();
        
        c.Register<B>();
        c.Register<BFactory>(
            setup: Setup.With(asResolutionCall: true) // fix3: makes BFactory a dynamically resolvable - the fix is here and not in B because the BFactory is already loops to AFactory and making B dynamic is not enough
        );
        
        c.Register<IrrelevantService>();
        
        var af = c.Resolve<AFactory>();
        Console.WriteLine(af);
    }
    
    public class AFactory {
       public AFactory(Func<A> getA){ }
    }

    public class BFactory {
       public BFactory(Func<B> getB, AFactory aFactory){ }
    }

    public class A { 
       public A(IrrelevantService service, BFactory bFactory){ }
    }
    
    public class B {}
    public class IrrelevantService {}
}