Learn to use Visual Studio, Visual Studio Online, Application Insights and Team Foundation Server to decrease rework, increase transparency into your application and increase the rate at which you can ship high quality software throughout the application lifecycle
Remember the last time you worked with unfamiliar code? It can seem like it takes forever to understand how to change it and even longer to see the potential impact. What if you had a picture that shows how the code is organized and gives you more information about how changes might affect it?
Good news! In Visual Studio 11 Beta, you can visualize your code and understand its relationships. You can create, read, and edit dependency graphs faster and easier. Here's an example:
Note: You can create graphs with Visual Studio 11 Ultimate. You can read and edit graphs with Visual Studio 11 Premium and Professional. To download the Visual Studio 11 Beta ALM virtual machine, which includes the Ultimate version, and hands-on-labs, see Brian Keller's post here.
This blog post covers the following scenarios and uses the .NET Pet Shop 4.0 sample application in its examples:
To magnify the examples, just click them.
Getting the Big Picture
To get an overview of the code, follow these steps:
The first time you generate a graph, it might take a little while. Visual Studio builds the solution, analyzes the binaries produced from each project, and indexes relevant details to generate future graphs faster. If you don't need a graph for the entire solution, you can also speed up this process by visualizing only those parts that you care about or narrowing the scope of your solution. To learn more, see Focusing on the Details and How to: Visualize Code by Generating Dependency Graphs.
This example shows the top-level assemblies in the sample solution. Everything outside the solution, like platform dependencies, is found in Externals:
To save the graph:
Visual Studio saves the graph to the Solution Items folder.
Browsing Dependency Graphs
The resulting graph can seem large and overwhelming. You have several ways to explore the graph:
For other ways to explore graphs, see How to: Browse and Rearrange Dependency Graphs. To see more mouse and keyboard gestures, create a blank graph, and then choose the Help links on the graph:
Examining Items and Relationships
A rectangle, or node, represents an item on the graph. An icon identifies the item's type. To see what the icons mean, use the Legend:
Some items contain other items, for example, assemblies contain namespaces, which contain types, and so on. To expand these containers, or groups, move the mouse pointer over the left part of a node. If a appears, choose it.
To expand and collapse groups, use these keyboard shortcuts:
If an expanded group seems too big, you can drag its contents to rearrange them. The layout of the surrounding nodes and links adjust automatically:
Layout Before Rearranging
Layout After Rearranging
Arrows, or links, between nodes represent relationships. If there are multiple relationships between two nodes, a single aggregate link combines all those relationships into one link.
To learn more about a relationship between two nodes:
The following example shows two assemblies. The tooltip tells you that the aggregate link represents multiple relationships: Calls, References, and Return. The denser the link, the more relationships it represents:
To see all the links between the items in these assemblies, make sure the graph shows all cross-group links:
Expand the assemblies and namespaces to see the links between their contents:
Focusing on the Details
Now, suppose you only care about a specific area of code because you have to fix a bug or update some functionality. You want to know how that change affects other parts of the code. For example, maybe you want to update the CreditCardInfo constructor to accept an authorization code. There are several ways to find that code:
Finding Items on Graphs
To find any items on the graph, use CTRL+F to open search:
The following matches appear selected on the graph:
To see the CreditCardInfo method definition, select it on the graph, and use F12 to open the code editor. Now, suppose you want to find other code that has dependencies on this method. To see items that have dependencies on a selected item, make sure that you can see the cross-group links for selected items. On the graph toolbar, open the list, and choose Show Cross-Group Links on Selected Nodes.
When you select the CreditCardInfo method, a link appears from PetShopWeb.dll and points at CreditCardinfo, showing that something in PetShopWeb.dll calls CreditCardInfo:
To find the methods in PetShopWeb.dll that call CreditCardInfo, expand PetShopWeb.dll incrementally and follow the links:
Finding Items in the Solution
To start from the solution, search or browse for the CreditCardInfo method in Solution Explorer. If you search, you find the following:
When you select the CreditCardInfo method, Visual Studio shows the method's definition in the code editor:
Without leaving Solution Explorer, you can find what calls CreditCardInfo method, what CreditCardInfo calls, or what uses it. Open the shortcut menu for the CreditCardInfo method, and choose Is Called By:
You see that two items that call CreditCardInfo. Select each item, and use F12 to see their definitions. You can create a new dependency graph that shows only these items and their relationships with CreditCardInfo:
which produces the following graph:
To see the missing children in these groups, choose Refetch Children on each group. Visual Studio shows them on the graph:
To add more related items to the graph, open the shortcut menu for a node, follow these steps:
Communicating the Changes
Now, suppose you come across dependencies on the graph that shouldn't exist. Suppose you want to propose changes to these dependencies and use the graph to discuss your suggestions with your team. You can edit the graph several ways:
To learn more about how to edit dependency graphs, see How to: Edit and Customize Dependency Graphs.
To let us know what you think:
Nice. It is possible to get a graph of "basic blocks"? Would help immensely when working with large legacy code base.
Amazing. It'll make it much easier to refactor and combine used one time classes into a single C# file to depopulate assemblies used only one place.
Ahh the color...it hurts my eyes....stop it please. Didn't you guys get the memo....Dev11 means using gray.
Now I have to go to the eye doctor....please remove all color and only use gray....like you're supposed to be doing to be Metro!
Very useful thing. Thank you for this post!
@Tobias: Hi Tobias, thanks for your feedback. Which legacy code are you working with?
@Tom and Aviw: Thanks for the kudos!
@Allen: Visual Studio isn't all gray. :)