Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting dependencies between namespaces in .NET

Are there any utilities that can examine a set of managed assemblies and tell you whether any of the types in one namespace depend on any in another? For example, say I have a MyApp.BusinessRules namespace and don't want it to access directly anything in MyApp.GUI, but both namespaces are in the same assembly. My goal is to be able to write a custom MSBuild task that verifies that various coupling rules have not been broken.

So far the only tool I have come across that looks like it might do this is NDepend, but I am wondering if there is a simpler solution.

like image 801
Mark Heath Avatar asked Nov 20 '08 11:11

Mark Heath


2 Answers

So far the only tool I have come across that looks like it might do this is NDepend, but I am wondering if there is a simpler solution.

I am one of the developer of the tool NDepend. Please could you let us know what do you find complicated in NDepend and how you imagine a simpler solution for you?

NDepend comes with 3 different ways to do what you want: Dependency Matrix, Dependency Graph and also you can write some Code Rule over LINQ Query (CQLinq) and rules to detect cycle between namespaces, or enforce some particular dependencies.

For example, say I have a MyApp.BusinessRules namespace and don't want it to access directly anything in MyApp.GUI, but both namespaces are in the same assembly.

For that, the following CQLinq rule can be written, could it be any simpler than that?:

warnif count > 0
let businessRules = Application.Namespaces.WithNameLike("^MyApp.BusinessRules")
let gui = Application.Namespaces.WithNameLike("^MyApp.GUI")

from n in businessRules.UsingAny(gui)
let guidNamespacesUsed = n.NamespacesUsed.Intersect(gui)
select new { n, guidNamespacesUsed }
like image 113
Patrick from NDepend team Avatar answered Nov 20 '22 11:11

Patrick from NDepend team


I suspect NDepend is going to be the simplest way to go, to be honest.

However, if you really don't want bits of one assembly from referring to each other, you should almost certainly split the assembly up into more logical units.

like image 3
Jon Skeet Avatar answered Nov 20 '22 09:11

Jon Skeet