Understanding Class Coupling with Visual Studio Ultimate 2012

Hands-On Lab
Understanding Class Coupling with Visual
Studio Ultimate 2012
Lab version:
11.0.60315.01 Update 2
Last updated:
4/9/2013
CONTENTS
OVERVIEW ................................................................................................................................................... 3
EXERCISE 1: USING DEPENDENCY GRAPHS TO VISUALIZE CLASS RELATIONSHIPS ................... 3
EXERCISE 2: DISCOVERING CIRCULAR REFERENCES ...................................................................... 14
Overview
Understanding and communicating the relationship between classes in a complex project can be
difficult. In this lab, you'll use the DGML (Directed Graph Markup Language) diagrams in Visual Studio
Ultimate 2012 to drill down into an existing code base and figure out how types are related.
Prerequisites
In order to complete this lab you will need the Visual Studio 2012 virtual machine provided by Microsoft.
For more information on acquiring and using this virtual machine, please see this blog post.
It is recommended that you complete the “Code Discovery using the Architecture Tools in Visual Studio
Ultimate 2012” lab before exploring this lab.
Exercises
This hands-on lab includes the following exercises:
1. Using Dependency Graphs to Visualize Class Relationships
2. Discovering Circular References
Estimated time to complete this lab: 30 minutes.
Exercise 1: Using Dependency Graphs to
Visualize Class Relationships
In this exercise, you will learn how to generate and navigate a class dependency graph.
1. Log in as Julia. All user passwords are P2ssw0rd.
2. Launch Microsoft Visual Studio from Start | All Programs | Microsoft Visual Studio 2012 |
Visual Studio 2012.
3. In the Start Page, Click on Connect To Team Foundation Server…
Figure 1
Location of Connect to Team Foundation Server link
4. In Team Explorer – Connect, double-click on the Tailspin Toys project. Select Yes when asked to
confirm the connection.
Figure 2
Loading the Tailspin Toys project
5. Open the Source Control Explorer by clicking on the hyperlink on the Team Explorer | Home
screen.
6. In Source Control Explorer, navigate to Tailspin Toys | Development | Iteration 2 and doubleclick on the TailspinToys.sln file to open the Tailspin Toys solution.
Figure 3
Loading sample solution
7. Rebuild the solution (Build | Rebuild Solution from the main menu). This step may take a few
minutes to complete.
8. Create a new dependency graph (Architecture | Generate Dependency Graph | For Solution).
The dependency graph is stored in a Directed Graph Markup Language format (hereafter
referred to as DGML), which allows you to work with it using Visual Studio 2012 as well as other
standard tools.
Note: Generating the dependency graph for all projects in the solution may take a minute to
complete. When you generate a dependency graph for the first time, a code index is created
for all the dependencies that are found, which helps improve the performance of subsequent
operations.
Figure 4
Dependency graph for entire solution
9. Select and expand the Tailspin.Model.dll assembly node to reveal the contained namespaces.
Figure 5
Expanding a graph node
10. Select and expand the Tailspin.Model namespace node.
Figure 6
Expanding a graph node
Note: Once you expand the Tailspin.Model namespace node, you will see a bunch of classes
that are likely too small to read without zooming in.
11. Select the Quick Clusters Layout to view all classes and their relationships at a glance.
Figure 7
Quick Clusters Layout option
Figure 8
Quick Clusters view of generated class dependency graph
Note: The generated dependency graph views that you see may be different from the
screenshots shown in this lab manual. You may need to perform additional zooming, scrolling,
and visually searching for objects specified in the lab steps.
12. The Quick Clusters layout view of this class dependency graph only gives us an idea of the
number of classes in all TailspinToys projects and their degree of dependency between each
other. This is of limited usefulness to us, so let’s go ahead and find a specific node by searching
for the Product class. There are two options that you can use to achieve this task, represented
by the next few steps.
13. The first option is to manually zoom in towards the center of the graph by using the mouse
scroll wheel until you can clearly see the Product class.
Note: Zooming can also be done using the Zoom drop down.
Figure 9
View controls
14. The second option is to perform a search of the graph. Press Ctrl+F to show the search box, type
“Product” followed by the Enter key. Press the F3 key to cycle through search results until the
Product class is highlighted.
Note: By default, search includes items in collapsed groups. However, if an item is in a
collapsed group that was never expanded, the item might not be found. This is why we
expanded a couple of groups in the previous steps. This would also be important to do before
sending a DGML file to another Visual Studio user.
Figure 10
Searching for the Product class
Note: Gray lines of varying thicknesses represent the magnitude of relationship
interdependencies between nodes, with thicker lines equating to more relationships. The
darker gray lines represent the direct relationships between the Product class and other
classes and groups on the graph.
15. Selecting the Product class node from the graph helps highlight its direct relationship to other
classes, but it is still difficult to distinguish between incoming and outgoing dependency
relationships. To help visually make this distinction, change the graph layout to Top to Bottom
Layout.
Figure 11
Selecting Top to Bottom layout
Figure 12
Result of Top to Bottom layout
16. The top to bottom layout shows incoming dependency relationship lines on the top and
outgoing lines on the bottom. This is an improvement, but there is still some visual noise from
dependency relationships to other groups, i.e. the relationship that the Product class has to the
Tailspin.Test.Model assembly. Select the Hide All Cross-Group Links option from the Layout |
Advanced menu.
Figure 13
Hiding all cross-group links
Figure 14
Graph view without cross-group links shown
17. Return to the Quick Clusters layout.
Figure 15
Graph view using Quick Clusters layout, also without cross-group links
18. Locate and hold the mouse cursor over the relationship line that connects the Product and
OrderLine class nodes.
Figure 16
Relationship information between classes
19. To view the contributing links for the relationship, right-click on the relationship line and select
Advanced | Show Contributing Links on New Code Map.
Figure 17
Showing contributing links
20. The new graph shows all of the OrderLine class members that contribute to the overall
incoming relationship to the Product class.
Figure 18
Detailed view of relationship between OrderLine and Product classes
Exercise 2: Discovering Circular
References
In this exercise, you will learn how to use the circular reference analyzer in order to discover classes that
are strongly coupled to each other.
1. Return to the original class dependencies graph (AssemblyDependencies1.dgml) and make sure
that the Product class node is visible.
Figure 19
Dependency graph with Product class node selected
2. Select the Legend button.
Figure 20
Legend button location
3. Click the + button at the bottom of the Legend box to view the available options to add to the
diagram.
Figure 21
Location of + button on Legend
4. Select Analyzer | Circular References to add the analyzer to the dependency graph.
Dependency graph analyzers will continue to run after you apply them, even if you change the
graph.
Figure 22
Adding the Circular References analyzer
5. If the Product node is selected, go ahead and click on a blank spot within the graph. Note that
there are red outlines around types that are strongly coupled. At this point, you could continue
to investigate the highlighted relationships in an effort to simplify them using the tools explored
in the previous exercise.
Figure 23
Dependency diagram showing circular references between classes
To give feedback please write to VSKitFdbk@Microsoft.com
Copyright © 2016 by Microsoft Corporation. All rights reserved.