How to: Explore Code with Dependency Graphs
In Visual Studio Ultimate, use dependency graphs to help you learn more about the organization and relationships in existing code. These graphs represent structures as nodes and relationships as links, which appear as arrows between nodes. To generate a graph, see How to: Generate Dependency Graphs for .NET Code.
The following example shows how the graph represents structures and relationships as nodes and links:
Nodes and links on a graph
The following example shows how the graph represents structures and containment relationships as groups:
Grouped nodes on a graph
Upozornění
A graph shows only those dependencies for code that builds successfully. Components and their contents will not appear if build errors occur for those components. Therefore, make sure that a component actually builds and has dependencies on it before you make architectural decisions based on the graph.
In This Topic
Explore dependencies on the graph
Find areas of complexity
Explore Dependencies on the Graph
Dependencies on the graph are represented by the following kinds of links:
An individual link represents a single dependency between two nodes.
An aggregate link represents all the dependencies that travel in the same direction between two groups.
A cross-group link is just a link between two nodes across different groups.
Poznámka
By default, the graph shows cross-group links only for nodes that are selected. To show these links at all times, on the graph toolbar, click the drop-down list next to the Change how links are rendered on the graph button, and then click Show All Cross-Group Links. If the graph toolbar is not visible, on the View menu, point to Toolbars, and then click Directed Graph.
Use the following tasks to explore the dependencies that interest you:
To |
Follow these steps |
---|---|
See a list of dependencies that a link represents |
|
Find a node by name Use to search through multiple levels of grouped nodes. |
|
Find the source or target of a link Use this method when there are too many links to easily see the source or target of a specific link. |
|
Examine the dependencies across nodes in different groups |
|
Examine the dependencies behind an aggregate link
Note
This works only when each end of the link is connected to an assembly. If there is no assembly information, then a graph cannot be created.
|
|
Find Areas of Complexity
Use a dependency graph to identify areas of code that you might be able to improve. A well-formed dependency graph will show a minimal number of dependencies. For example, you can:
Find code that has loops or circular dependencies.
Examine these areas to see whether you can simplify them. Consider whether you can break these cycles.
Find code that has too many dependencies.
Examine these areas to see whether they are performing too many functions or to determine the impact of changing these areas. To make code easier to maintain, change, test, and reuse, consider whether you can refactor these areas so that they are more clearly defined, or whether you can merge code that performs similar functions.
Find code that has no dependencies on it.
Examine these areas to see whether they are necessary or whether you should remove this code.
You can run specific analyzers on these graphs to help you perform these tasks. These analyzers continue to run after you apply them. If you change the graph, any applied analyzers will automatically reprocess the updated graph. To stop running an analyzer, right-click the analyzer in the Legend box, and then click Delete.
Poznámka
If you have a very large graph, running an analyzer might cause an out of memory exception. If this occurs, edit the graph to reduce its scope or generate a smaller one, and then run the analyzer.
To find areas of complexity in code
Make sure the graph layout is set to tree layout or quick clusters layout.
For more information, see How to: Browse and Navigate Graph Documents.
Right-click the graph, point to Analyzers, and then click one of the following:
Analyzer
To identify nodes that
Circular Reference
Have circular dependencies on each other
NoteCircular dependencies inside the Generics group are not shown on the graph when you expand the group.Find Hubs
Are in the top 25% of highly-connected nodes
To select these nodes and hide all others on the graph
Right-click the graph, and then click Show Advanced Selection.
In the Selection box, expand Properties, and then Hub.
Under Hub, click True.
The top 25% of the most highly-connected nodes are selected.
Right-click the graph, point to Selection, and then click Hide Unselected.
The graph hides the unselected nodes, and the analyzer identifies new nodes as hubs.
You can also identify dense areas of dependencies by switching the graph layout to dependency matrix view. For more information about dependency matrix view, see How to: Browse and Navigate Graph Documents.
Unreferenced Nodes
Do not have references from any other nodes.
CautionVerify each of these cases before assuming that the code is not used. Certain dependencies such as XAML dependencies and run-time dependencies cannot be found statically in the code.
See Also
Concepts
How to: Generate Dependency Graphs for .NET Code