Class Dependency Diagram

With NDepend in Visual Studio it is easy to quickly generate and browse any class dependency diagram you need to understand how your code is actually structured.

Class Dependency Diagram of All Classes in a Visual Studio Solution

A Visual Studio solution contains some projects, projects contain some namespace and namespace contain some class. It is often useful to browse a class dependency diagram made of all classes in solution that reflects this hierarchy. The NDepend graph can scale till hundreds of thousands of elements so even on large Visual Studio solutions such overall class dependency diagram can be quickly obtained and browsed.

To generate such overall class dependency diagram you can click All ... types in the navigation bar. For example here we have more than 3.700 classes that can be browsed live.

Obtaining and Browsing a Graph of more than 3.700 Classes

Notice that element box size is proportional to the number of lines of code of the corresponding project, namespace, class.

Navigating such large class dependency diagram is actually easy thanks to search and highlight facilities explained in the next sections.

Searching in a Class Dependency Diagram

With the Search feature classes can be found by name. For example in the animation below we search for all classes related to Model and Order, and then click the button keep to generate a graph made of only classes matched by the search:

Searching by Name in a Large Classes Dependency Diagram

Explore Dependencies between Classes

When hovering or selecting a class, classes that use it are highlighted in green, classes that are used are highlighted in blue and classes that both use and are used by are highlighted in red. This color scheme is reminded by the Help panel. The picture below shows classes that depend on the class Order:

Classes Dependents of the Selected Class are Highlighted

Clicking the button keep dependents in the navigation bar generates a diagram made only of classes highlighted.

Note that several classes can be selected: in this situation usage highlighting is adjusted. The picture below shows classes that depend on the classes Order, Discount and Customer:

Classes Dependents of the Selected Classes are Highlighted

Drag and Drop Classes from the Visual Studio Solution Explorer

One quick and easy way to generate a useful class dependency diagram is to drag and drop their source files or their folders from the Visual Studio Solution Explorer to the NDepend dependency graph:

Drag and Drop Classes from the Visual Studio Solution Explorer to the Graph

Dependency Diagram of Classes of a Component

We have explained how to obtain a dependency diagram made of all classes in a Visual Studio solution. However it is often useful to restreint the class set to classes in one or several project(s) or namespace(s).

There are many ways to obtain such children class dependency diagram. For example by expanding parent elements. There are two expand facilities:

  • The right-top triangle in parent boxes
  • The expand button in the navigation bar. If no element is selected this button expands all parent elements actually collapsed. Else this button expands only selected elements actually collapsed.

Notice that the expand button can be clicked several times to first expand till the namespace level, then expand till the class level and then expand till the method and field level.

In the picture below we expanded the project Nop.Core until classes, then we changed the Group-By settings to get rid of the parent project box and keep only children classes grouped per namespaces. Dependents highlighting is then useful to browse which classes depend on selected classes:

250 Classes Dependency Diagram of the project Nop.Core

Class Inheritance Diagram

When a class is selected, the button derived class is shown in the navigation bar. Clicking this button edits a code query that matches derived classes and generate a graph of those derived classes:

Class Inheritance Diagram

The same way when an interface is selected the button interface implementer is shown in the navigation bar and clicking this button edits a code query that matches classes that implement the interface and generate a graph with those classes:

Diagram of Classes that Implement an Interface

Diagram of Classes Using or Used By a Class

When a class is selected the navigation bar shows the buttons callers, callers and callers and callees. Clicking one of those buttons instantly generates a graph of classes using and/or used by the selected class. In the navigation bar the combo box call depth let's choose the depth of call:

  • Direct: Only classes that directly use or are directly used by the selected class
  • Max: Classes that directly use or are directly used by the selected class, and (recursive) all classes that use or are directly used these classes.
  • Level 2, 3 ...: Classes that directly use or are directly used by the selected class, and (recursive until level) all classes that use or are directly used these classes.
Generate a Diagram of Classes Using a Class

Class Coupling Diagram

Double clicking an edge between two classes generates a graph made of methods and fields involved in the dependency between the two classes. Alternatively just clicking an edge select both edge afferent and efferent elements and the menu coupling graph is shown in the navigation bar.

Generate a Diagram of Classes Involved in the Dependency betwen Tow Projects