You are here
Home > Visual Studio 2012 > Visual Studio 2012 Tips Part 4 – Understand Your Code With Dependency Graphs

Visual Studio 2012 Tips Part 4 – Understand Your Code With Dependency Graphs

Fallback Image

Visual Studio 2012 Solution ExplorerHave you ever had to take over a project from a developer who has left the company or did work as an outside contractor and now you need to enhance / add to that code?

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.

Visual Studio 2012 Generate Dependency Graph

After you do this, Visual Studio will start generating your Dependency Graph. You will see the following progress screen.

Generating dependency graph for solution

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.

Dependency Graph Expanded Visual Studio 2012

Hovering over an assembly, you will see that it can be expanded to see it’s namespace, classes and other members.

Dependency Graph Class Expanded

If you click on an item, you will see a short code preview…

Method Selected Code PreviewDependency Graph

…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)

Dependency Graph Right Click go to definition

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.

Dependency Graph Find all references

Rearranging items on the graph is also easily done by simply dragging them to the position you want on the dependency graph.

Dependency Graph drag to rearrange

Visual Studio also provides analysers that you can use to help you identify potential problems. To do this, select Layout -> Analyzers.

Dependency graphs 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.

Circular dependency identified

You will also notice that your Dependency Graph has an Externals Group. This displays all the external dependencies for your application.

Dependency graph externals

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.

Reference: alvinho

Dirk Strauss
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.

Similar Articles

Top