1.3.9 Translating Sequence Diagrams Into C#

Video: Writing Code from Sequence Diagrams (33:07)
A Sequence Diagram Gives You an Ordered Call Plan

A UML sequence diagram shows:

  • the objects involved;
  • which object receives each message;
  • the method name;
  • the order of the interactions.

That makes it a useful guide for C# method-call structure.

Consider this conceptual interaction:

Sequence diagram showing controller1 : Controller team1 : Team, then DisplayName().
Controller1ControllerTeam1Team.
  • New
  • Changed
  • Removed

The message says:

Translate the Receiver First

The message ends at:

Sequence diagram showing controller1 player1 : ShowTeamName(), then player1 team1 : DisplayName().
Controller1Player1ShowTeamName.
  • New
  • Changed
  • Removed

This does not mean both calls must appear next to each other in one method.

The diagram tells you that the second call occurs while the Player method is executing.

A conceptual C# structure can be:

class Player
{
    public Team team;

    public void ShowTeamName()
    {
        this.team.DisplayName();
    }
}

and the original caller can contain:

player1.ShowTeamName();

The nested placement preserves the interaction.

Match Calls to Methods that Already Exist in the Design

If the sequence diagram contains:

Sequence diagram showing sequence diagram message, then C# method call, then runtime method execution.
SequenceDiagramMessage.
  • New
  • Changed
  • Removed

The debugger provides the third view.

Translation Means Preserving the Interaction

The goal is not line-for-line conversion of drawing geometry.

The goal is to preserve:

That is the common meaning shared by the UML sequence diagram and the C# source.