简体   繁体   English

如何使用 NDepend 查找导致两种类型交织在一起的成员?

[英]How can I use NDepend to find the members that cause two types to be intertwined?

I'm trying to disentangle a lot of heavily interdependent types in one of my company's .NET assemblies.我试图在我公司的一个 .NET 程序集中解开大量相互依赖的类型。

It seems like the first step would be to take a couple of classes and look at which members of each cause them to be interdependent.似乎第一步是采取几个类并查看每个类的哪些成员导致它们相互依赖。

How can I do this with NDepend?我怎样才能用 NDepend 做到这一点? If I have TypeA and TypeB, what CQLinq can I write to ask for all the methods in TypeA that use TypeB, and all the methods in TypeB that use TypeA?如果我有TypeA和TypeB,我可以写什么CQLinq来要求TypeA中所有使用TypeB的方法,以及TypeB中所有使用TypeA的方法?

First you can use this query to match all types interdependent (you might need to increase the CQLinq run timeout since it is a O(nbTypes^2) query)首先,您可以使用此查询来匹配所有相互依赖的类型(您可能需要增加 CQLinq 运行超时,因为它是一个 O(nbTypes^2) 查询)

from t1 in Application.Types
from t2 in Application.Types
where 
  t1 != t2 && 
  string.Compare(t1.FullName, t2.FullName) == 1 &&
  t1.IsUsing(t2) && t2.IsUsing(t1)
select new { t1, t2 }

NDepend 类耦合相互依赖纠缠

Then for each pair, you can export both types to matrix column and row as shown on the screenshot.然后对于每一对,您可以将两种类型导出到矩阵列和行,如屏幕截图所示。 Then right click the black cell (black coz types are interdependent), click Open this dependency然后右击黑色单元格(黑色coz类型是相互依赖的),点击打开这个依赖

NDepend依赖矩阵类耦合相互依赖纠缠

then click this menu然后点击这个菜单NDepend 依赖矩阵删除空行和列

et voilà, the culprit is now obvious等等,罪魁祸首现在很明显

  • blue cell means item in column uses item in row,蓝色单元格表示列中的项目使用行中的项目,
  • green cell means the opposite,绿色单元格表示相反,
  • much more cells of one color than the other (which is the common pattern) clearly indicates the weak dependency direction to eventually discard一种颜色的单元格比另一种颜色多得多(这是常见的模式)清楚地表明最终丢弃的弱依赖方向

NDepend 依赖矩阵类耦合

btw the first query can be refined this way, and methods groups matched in the query result can be also exported to matrix in a right-click menu btw 第一个查询可以这样细化,查询结果中匹配的方法组也可以在右键菜单中导出到矩阵

from t1 in Application.Types
from t2 in Application.Types
where 
  t1 != t2 && 
  string.Compare(t1.FullName, t2.FullName) == 1 &&
  t1.IsUsing(t2) && t2.IsUsing(t1)
select new { t1, t2,
methodsOf1Using2 = t1.Methods.UsingAny(t2.Members),
methodsOf2Using1 = t2.Methods.UsingAny(t1.Members)
} 

NDepend 代码查询结果依赖类耦合

Related doc to deal with dependencies:处理依赖项的相关文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM