How to: Explore Code with Sequence Diagrams
In Visual Studio Ultimate, generate a sequence diagram to help you visualize and understand how the code implements a particular method. A sequence diagram describes an interaction between objects as a series of lifelines and messages. Lifelines represent instances of objects, and messages represent method calls between those objects. You can generate sequence diagrams from Visual C# .NET or Visual Basic .NET code.
A generated sequence diagram shows elements similar to those on a UML sequence diagram. A sequence diagram that is generated from code exists independently of a UML model and can be added to any .NET project. By contrast, a UML sequence diagram that is drawn by hand is always part of the model in which you created it. For more information, see UML Sequence Diagrams: Reference and the following video:
MSDN How Do I Series: How to Generate UML Sequence Diagrams.
To generate a sequence diagram from code
In Visual Studio, open the code file that contains the method definition.
Right-click anywhere in the method definition, and then click Generate Sequence Diagram.
In the Generate Sequence Diagram dialog box, in the Maximum call depth box, specify the maximum level of calls that you want to see.
For example, if you want to see only the first level of calls, specify a maximum call depth level of 1.
Under Include calls in, choose a scope from the following table:
For example, you can start with a simple sequence diagram by examining only those method calls to items in the immediate project. You can expand the scope of the diagram by generating a new one that shows calls to items across the solution. If you want to see calls to items that are not in the solution, you must generate a diagram that includes external references.
Under Exclude calls to, you can simplify the diagram more by omitting calls to the following items:
To include calls to these items, clear their corresponding check boxes.
When you are finished, click OK.
Visual Studio generates a sequence diagram and adds it to the corresponding project. For information about issues with generating sequence diagrams, see Troubleshooting Sequence Diagrams.
To identify and communicate changes that you might want to make without affecting the code, you can explore and modify a generated sequence diagram. For example, you can add new lifelines, messages, interaction uses, and fragments. To create and track tasks for work that you want to perform on the corresponding code, you can create or link work items to diagram elements. For more information, see How to: Link from Model Elements to Work Items.
A sequence diagram can help you explore the code and to present it to others. To achieve the most useful diagram for these purposes, experiment with different scopes when you generate the diagram. You can also modify the diagram after you have generated it. For example, you can delete messages that are not interesting for presenting and discussing the design.
You can make the following changes to the sequence diagram.
You can change the diagram, adding and deleting elements to help you consider alternative designs. You can also copy parts of the diagram to a UML sequence diagram that you have created in a UML modeling project. That enables you to associate the lifelines with the types, components and actors of the UML model.
The following table summarizes changes that are useful for describing design proposals. For more details, see UML Sequence Diagrams: Guidelines.
To help plan the work of implementing a change, you can link elements of any kind on a sequence diagram to work items in Team Foundation Server. For more information, see How to: Link from Model Elements to Work Items.
The following guidelines might be useful when you are exploring code with generated sequence diagrams.
Exploring the Code
Generate dependency diagrams to obtain an overall picture of the major groupings and dependencies in a large body of code. Then generate sequence diagrams to focus on individual parts of the design. For more information, see How to: Generate Dependency Graphs for .NET Code.
Interpreting the Diagram
The algorithm that creates the diagram performs a symbolic execution of your code. There are some situations that this technique does not cover. In particular, these situations include:
Two or more lifelines might appear in the diagram where your executing code only deals with one instance. In general, a separate target lifeline is generated for each message.
The type of a lifeline is the type declared in the program code, even if your code always uses a subtype.
Focusing on an Aspect of the Design
Choose one aspect of the design to focus on. You can generate different diagrams to focus on different aspects, and make copies of the diagram files at any stage of your work.
To get a useful diagram that clearly exposes an aspect of the design, you might have to experiment with the namespace filters and generation.
Delete messages that are not relevant to the focus aspect, and expand relevant messages to show more detail.
Use the Collapse command to group lifelines that represent a collaborating group of objects, unless their internal interactions are interesting for the aspect you are focusing on.
Updating the Design
You can work collaboratively on a diagram using version control. We recommend that you lock a diagram when you are editing it so that only one person can edit it at a time.
Using a generated sequence diagram, you can often identify features of the existing code that make it difficult to change or adapt. For example, you might notice that one lifeline sends all the messages, but others just respond to messages, without sending many of their own. Consider whether the work being done by this object can be more effectively divided between the other objects. Each class of object should have a single clearly-defined task. In general, this policy makes it easier to identify which parts of the program must be changed when the users' needs change, or to rearrange the parts in a different variant of the application.
When you have a diagram that focuses on a specific aspect of the design, make copies of it and edit them to create alternative proposals for changes.
You can copy elements of a generated sequence diagram into a UML sequence diagram. You must first create the UML sequence diagram inside a UML modeling project. There, you can model different views of the updated design, and you can link the lifelines to components or classes in the model. You can also extend the interaction to include external actors.