c# - 如何解决 Funq IoC 中的循环依赖?

标签 c# ioc-container servicestack circular-dependency funq

我有两个类需要相互引用。

class Foo
{
    public Foo(IBar bar) {}
}

class Bar
{
    public Bar(IFoo foo) {}
}

当我这样做时:

container.RegisterAutoWiredAs<Foo, IFoo>();
container.RegisterAutoWiredAs<Bar, IBar>();

当我尝试解析任一接口(interface)时,我得到一个循环依赖图,这会导致无限循环。在 Funq 中有解决这个问题的简单方法吗?或者您知道解决方法吗?

最佳答案

您始终可以(我会说在所有容器中)依赖 Lazy 作为依赖项,这将产生所需的结果。在 Funq 中:

public Bar(Lazy<IFoo> foo) ...
public Foo(Lazy<IBar> bar) ...

container.Register<IBar>(c => new Bar(c.LazyResolve<IFoo>());
container.Register<IFoo>(c => new Foo(c.LazyResolve<IBar>());

关于c# - 如何解决 Funq IoC 中的循环依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12301792/

相关文章:

optimization - 随时间收集服务性能指标

c# - 简单注入(inject)器/IoC - 队列处理器的 Windows 服务和请求周期

c# - ServiceStack AutoQuery AutoFilter Like 操作数

c# - 服务堆栈 "Declaration referenced in a method implementation cannot be a final method"

c# - 在 C# 4.0 Winform 中为程序(如 MS Word)仅打开一次文件

java - 为什么 Spring 不将映射注入(inject)到子 Struts 2 Action 类中,而是注入(inject)父对象的对象?

c# - 从 ContentControl 中使用的 Ioc 的 viewModel 中释放内存

c# - 如何设置ComponentArt datagrid添加bootstrap Grid系统datagrid?

C# doubles 显示逗号​​而不是句点

c# - XAML - ListViewItem - IsEnabled 绑定(bind)