MVC 5 这会导致无限循环吗?

MVC 5 Could this result in an infinite loop?

我正在编写一个 MVC 5 互联网应用程序,并且有一个关于潜在编码缺陷的问题。

我有一个 AccountService class,它有一个在构造函数调用中初始化的 MapLocationMarkerService 对象。 MapLocationMarkerService 有一个构造函数参数是 AccountService.

当我创建一个 AccountService 对象时,我在初始化 MapLocationMarkerService 对象时传入当前 AccountService 对象。

这会导致无限循环吗?

否:

public class A
{
  public B b;

  public A()
  {
    b = new B(this);
  }
}

public class B
{
  public A a;

  public B(A _a)
  {
    a = _a;
  }
}

这称为循环引用。这可能表明服务没有正确解耦。您可以将一些方法重构到另一个服务中 class 以删除循环引用。

This文章可以帮助您理解和解决问题。