As you then probably know, it takes a bit of code surfing to get to know the code and how everything fits together. It is also not immediately clear which dependencies are present in your code. Luckily Microsoft has built a tool that can help.
With Visual Studio Ultimate, you can create Dependency Graphs from your code to help you visualise how that code is organised and it also helps you understand the dependencies more easily. (Compare Visual Studio editions)
It is an excellent tool for working with unfamiliar code. Dependency Graphs give you an overview of all the code in your solution. With this post I will give you an overview of what you can do with Dependency Graphs.
For a more in-depth article, see Visualize Code Dependencies on Dependency Graphs.
In order to generate a dependency graph, click on the Architecture menu item and then Generate Dependency Graph -> For Solution.
After you do this, Visual Studio will start generating your Dependency Graph. You will see the following progress screen.
After it has completed, your Dependency Graph will appear as a new file with a .dgml extension in a new Tab in Visual Studio. For the purposes of this post, I simply downloaded a project contributed to the Samples section of the Visual Studio Developer Centre.
Hovering over an assembly, you will see that it can be expanded to see it’s namespace, classes and other members.
If you click on an item, you will see a short code preview…
…which is an excellent way to prompt you to realise that you can view the underlying code easily. You can therefore quickly go from graph to code by double clicking the item in the dependency graph or by right clicking on the item and selecting “Go To Definition”. (Or typing F12 will also work)
The solution I am using as an example is rather small, so you can imagine what a real time saver dependency graphs are with big complex solutions. Next, the dependency graph allows you to find all references easily by selecting that option from the context menu just below the “Go To Definition” item. This highlights referencing items in green, making them easily identifiable in the graph.
Rearranging items on the graph is also easily done by simply dragging them to the position you want on the dependency graph.
Visual Studio also provides analysers that you can use to help you identify potential problems. To do this, select Layout -> Analyzers.
You can for example choose the Circular References Analyzer. (As per Wikipedia: A circular reference is a series of references where the last object references the first, resulting in a closed loop. Circular references can appear in computer programming when one piece of code requires the result from another, but that code needs the result from the first.) For further reading on the topic of Circular References, see What is the best way to resolve circular references.
You will also notice that your Dependency Graph has an Externals Group. This displays all the external dependencies for your application.
Lastly, you can also explore dependencies by examining the links between items in the graph. The thicker the link, the more dependencies, and you can get more information on a link by examining it’s tool tip. As you can see, the Dependency Graph in Visual Studio 2012 is a very useful tool for quickly acquiring an overview of the project and how everything fits together. I suggest creating a few Dependency Diagrams from projects you are well familiar with and know. This will help you understand the Graph too.