1

I'm trying to disentangle a lot of heavily interdependent types in one of my company's .NET assemblies.

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? 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?

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211

1 Answers1

1

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)

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 classes coupling interdependent entangled

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

NDepend dependency matrix classes coupling interdependent entangled

then click this menu NDepend dependency matrix remove empty rows and columns

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 dependency matrix class coupling

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

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 code query result dependencies class coupling

Related doc to deal with dependencies:

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92