An Introduction To Using NDepend In Visual Studio

Introduction

 
In today’s article, we will look into an extremely useful tool that integrates into our Visual Studio environment. This tool is called NDepend, and it gives us valuable insights into our application in terms of code analysis, dependency graphs to visualize the flow of our application, code differences using a baseline, build process health to name a few.

How to get NDepend?

 
We can browse to the NDepend site at the below URL,

https://www.ndepend.com/
 
An Introduction To Using NDepend In Visual Studio
 
From here we can download the 14-day trial version.
 
An Introduction To Using NDepend In Visual Studio

Once you have downloaded the zip file, you can install the tool for your desired version of Visual Studio. I installed the version for Visual Studio 2019, to run it with my installation of Visual Studio 2019 Community Edition.
 
An Introduction To Using NDepend In Visual Studio
 
An Introduction To Using NDepend In Visual Studio
 
Once the installation is complete, we are ready to test the tool.
 

Using NDepend


I have created a new .Net Core 3.1 MVC project called “WebApplicationCore” inside a solution named “WebApplicationCoreSol” using the default template as below,
 
An Introduction To Using NDepend In Visual Studio
 
Next, we verify it runs correctly,
 
An Introduction To Using NDepend In Visual Studio
 
Next, we will see how to use NDepend to analyze this project’s code. Select “Extensions”, “NDepend” and then “Attach New NDepend Project to Current VS Solution” as below,
 
An Introduction To Using NDepend In Visual Studio 
An Introduction To Using NDepend In Visual Studio
 
Then click the “Analyze 2 .NET Assemblies” button.
 
When the processing is complete, we see the below screen,
 
An Introduction To Using NDepend In Visual Studio
 
From here, click the “View NDepend Dashboard” option. We will then see the below,
 
An Introduction To Using NDepend In Visual Studio
 
A report will also open in the browser as below,
 
An Introduction To Using NDepend In Visual Studio
 
Clicking on the “Dependency Graph” will show the dependencies of the application and how they relate to each other. This is extremely useful for large solutions with multiple projects and complex dependencies.
 
An Introduction To Using NDepend In Visual Studio
 
Coming back to the Dashboard in Visual Studio we can see the baseline is the same as the current code, as this was our first run.
 
An Introduction To Using NDepend In Visual Studio
 
Later, we can specify a different baseline for comparisons.
 
Let us look at the Rules. Click on the Ok rules number as below,
 
An Introduction To Using NDepend In Visual Studio

And you will see a list of rules that the code has been analyzed against. We can also write our own code rules. To see further details of the rules, simply hover over a rule in the bottom right window as below,
 
An Introduction To Using NDepend In Visual Studio
 
We have looked at two out of the numerous features in this tool. I would recommend you download the tool and look through the documentation to see the different options and reports available.
 
There is just one more thing I would like to mention today. We can also run the tool from outside of Visual Studio.
 
Simply open the zip file folder as below,
 
An Introduction To Using NDepend In Visual Studio
 
Run the “VisualNDepend” application and you will see the below,
 
An Introduction To Using NDepend In Visual Studio
 
From here you can start a new NDepend project or simply open the one we created earlier on our MVC solution,
 
An Introduction To Using NDepend In Visual Studio
 

Summary

 
In this article, we looked at a useful tool called NDepend. This tool helps us with code analysis to determine if our code meets certain standards. We also saw the dependency diagram to help us understand the relationship between entities. These are two of the simplest features of this tool. Once you download and go through the documentation you will find many other helpful features which can make your coding and coding process much more efficient. Happy coding!