Tools for detecting circular dependencies in depen

2019-07-08 01:39发布

Recently I started using dependency injection. In simple cases I didn't have any problems but with increasing complexity I found it difficult to quickly find circular dependencies.

Here's the example of circular dependency with constructor injection:

public class Class1
{
  public Class1(Class2 test2)
  { ... }
}

public class Class2
{
  public Class2(Class1 test1)
  { ... }
}

I use Unity IoC container. Circular dependency causes endless recursion and StackOverflowException to be thrown in the constructor.

Obviously this kind of dependencies is a code smell because it breaks single responsibility principle, so they should be localized and refactored.

Are there any tools for Visual Studio to detect these dependencies automatically?

0条回答
登录 后发表回答