1

I wanted to verify few design patterns in C# code by static analysis. I want to verify these using NDepend. The application is built with MVVM design style, so typical design patterns that I would like to verify are: 1. View-Model classes do not have any DependencyProperties (WPF elements), they should only be seen in View layer. 2. The View layer should not implement IDisposable etc

I have tried building queries in CQL to capture these details, but failed as I need to verify which level a class belongs to and also the properties of it. I understand nested query is not supported by CQL yet, so is there any way I can capture these details by NDepend? I am hoping I can somehow capture the results of multiple queries and perform intersection of them?

Thanks in advance, Shaun

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
Shaun
  • 199
  • 1
  • 10

1 Answers1

1

The easiest way to get a CQLinq rule that warns if A is using B (A and B can be Assembly/ Namespace/ Type/ Method/ Field) is to view A and B in the Dependency Matrix, and to Generate a code rule that warns if this dependency exists:

Generate a code rule that warns if this dependency exists

...and as a result, the following rule is generated:

Code rule genetaed

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • Thanks Patrick for the reply. I have tried doing this, but this is not exactly what I want. I would like to have a CQL query to verify if a DependencyPattern belongs to a particular set of classes (e.g. classes belonging to the View layer). So I have to check two things in the same query: 1) if the class belongs to a particular layer and 2) if the Dependency property exists. Can I do this in CQL? – Shaun Mar 17 '11 at 10:00
  • Could it be: WARN IF Count > 0 IN SELECT TYPES FROM "xxx" WHERE IsDirectlyUsedBy "yyy" OR IsDirectlyUsingBy "zzz" – Patrick from NDepend team Mar 17 '11 at 14:09
  • Thanks Patrick. It gave me an error. I think only Assemblies and namespaces are expected after FROM keyword. – Shaun Mar 17 '11 at 16:06
  • If some asm and namespace have the same name prefix them like: FROM "ASSEMBLY:XXX" or FROM "NAMESPACE:XXX" – Patrick from NDepend team Mar 19 '11 at 17:56