UML Modeling with Visio, Part 1
This is the first article in a two-part series whose aim is to explain the fundamentals of using Microsoft Visio to create UML diagrams for use with Visual Studio .NET. This first article explores some of the basics of Visio and UML diagrams, as well as how to create static diagrams within Visio. The second article details how to create dynamic diagrams in Visio and generate code from them in your language of choice.
The Enterprise Architect version of Microsoft Visual Studio .NET 2003 includes a copy of Microsoft Visio for Enterprise Architects (VEA). For those of you new to the product, Microsoft Visio is a drawing package that allows you to select predrawn shapes from stencils to draw and design different kind of diagrams such as flowcharts, network diagrams, and software diagrams.
Software application developers can model the application's design and functionality with Visio and Unified Model Language (UML) 2.0 through Visio's UML Model Diagram template. Also, Visio can perform reverse engineering on an implemented system and transform existing code into a UML model.
This two-part article series explains how to model different UML diagrams with Visio and how to transform these diagrams into code in a .NET programming language, thus reducing the work necessary to implement the modeled solution from scratch.
In this first article, I start with a brief overview of several UML diagrams and examine their place in the development cycle. I analyze how to design a university system using UML diagrams. The system will allow students to enroll in seminars and take courses. Courses are taught by specific professors. The system will also store both students' and professors' addresses. This article covers the development of the static diagrams for the university system.
To work with the solution in this sample, you should have
; Visual Studio .NET 2003 Enterprise Architect (which includes VEA)
; Visual Studio .NET and Microsoft Visio 2000 or later
The Sample Code
All the diagrams developed in this article are available for download from the ASP Today site. The download contains the file UniversityModel.vsd , which contains all the diagrams in
Microsoft Visio format.
The Unified Modeling Language (UML) was the result of the collaboration of three different modeling techniques that existed from 1980 to the mid-1990s, led by Grady Booch, James
Rumbaugh, and Ivar Jacobson, who were pursuing a common modeling notation. As industry involvement grew in the project, Booch, Rumbaugh, and Jacobson (known as "The Three Amigos") worked with Object Management Group (OMG) to have UML adopted as the standard industry modeling language, which finally occurred in 1997. Since then, UML has evolved to address changing industry needs and will continue to do so in the future. UML provides a common notation for specifying, constructing, and documenting systems that use object-oriented code, such as C# or Visual Basic .NET. UML defines a set of diagrams for Object-Oriented Analysis and Design (OOAD). These diagrams can be grouped into two distinct categories:
; Static diagrams for UML analysis represent the logical structure of information
objects. They are used to map the conceptual problem domain. Their purpose is not to
produce code, but to help developers understand the problem that needs solving.
These diagrams include
; Use case diagrams : Used mainly for enterprise viewpoint modeling to discover the
key functionalities of a system and how users (represented by actors) interact with it
; Static structure diagrams : Used to represent simplified or detailed class diagrams
because they identify objects and define objects characteristics such as attributes and
; Implementation (component and deployment) diagrams : Describe the
computational components in the system and how they are deployed
; Dynamic diagrams for UML design represent the behavior and activities of the
information objects. They are used to map the physical problem domain. These
; Class diagrams : Based on static structure diagrams, class diagrams are one of the
most important dynamic diagram types in UML. Class diagrams produced at this point
will be used to generate code.
; Interaction diagrams ( such as sequence and communication diagrams): Model the
behavior of use cases by describing the way groups of objects interact to complete a
; State diagrams (such as activity diagrams ): Represent the different internal
state and transitions of objects in the system.
The relation between the static and dynamic models is important. Static models represent concepts without much detail and cannot be accurate without associated dynamic models. Dynamic models, on the other hand, do not adequately represent considerations of structure and dependency management. Both types of diagrams need to be combined to model the best solution. Once you are working on dynamic diagrams, there is often too much emphasis on designing class diagrams rather than on other dynamic diagrams, such as interaction and communication diagrams. Interaction and communication diagrams, however, must not be undervalued. They are critical in the modeling process because they ultimately model the software behavior.
This article, Part 1, covers the static diagrams. I'll focus on the static structure diagram produced during the UML design to depict what is called the class diagram and the generation of code from it. Part II covers the dynamic diagrams in detail.
The entire UML specification and UML usage guidelines are available for viewing and download from the OMG site (see the Related Links section at the end of this article for the site's URL). One of the biggest advantages of designing class diagrams with Visio is that they can automatically be transformed into code: C#, C++, or Visual Basic .NET-the decision is yours. This code can be placed into independent files or inside a Visual Studio .NET project. In addition to the code itself, class diagrams can generate documentation to be saved as a report in Microsoft Word format, as you'll see later. These two topics are covered in Part II.
Note: The best diagram to convert into code is the class diagram, because it represents the whole system in great detail. Static diagrams, as well as other dynamic diagrams such as interaction and communication diagrams, cannot be directly converted into code. Visio UML Model Diagram Stencils
To work with UML diagrams, open Visio and choose UML Model Diagram under the Software
category. Note that depending on the version of Visio you are using, you may be able to select UML Model diagrams in U.S. or metric units.
If you don't see the window displayed in Figure 1 when you open Visio, go to the menu bar and select File --> New --> Software --> UML Model Diagram .
Figure 1. Visio Drawing Type window
This will create a new Visio document, as shown in Figure 2. On the left area of the screen is a window called Shapes that displays the different stencils available in the UML Model Diagram
component. These stencils contain drag-and-drop elements you can place on the different UML diagrams described previously.
On the bottom area of the screen is a window called Model Explorer . This window will prove to
be extremely useful when you are working with big models, as it allows you to see all the different objects and their properties, and all diagrams involved in a given project at a glance. You can also move, delete, or rename objects in Model Explorer, similar to the way you can in the Class view window in Visual Studio .NET. You can access the Model Explorer window at any
time by going to the menu bar and selecting UML --> View --> Model Explorer .
You can set the programming language for the code generation by going to the menu bar, selecting UML --> Code --> Preferences , and choosing the appropriate language from the
Target language drop-down menu.
Figure 2. UML stencils, Model Explorer, and the Drawing area
UML Analysis: Static Diagrams
Before you start adding diagrams to the model, you need to stop and think about how you want to organize the different information in the model you are about to build into folders, and where you want to save different types of diagrams and objects in different packages. This point is very important because Visio will use the final model structure as namespaces when the code is generated.
To create a new package, go to the Model Explorer window, right-click Top Package , choose
Package , and rename the new package Conceptual Model . All the different diagrams produced
during the UML analysis phase can be saved in the Conceptual Model folder.
Note that you can rename the pre-existing packages in this window by simply right-clicking the name and choosing Rename . You can rename the Static Model package, for example, to
something more meaningful to your business. Since the different examples in this article are based on a university system, I renamed it University Model . This means that the resultant
namespace will be called UniversityModel .
Below the different packages included in the model, the Model Explorer window lists the most
basic data types for some of the most popular .NET languages: C#, C++, and Visual Basic .NET. There may be occasions when you need to add new data types or interfaces to the language that you are working with. To add a new data type (or interface), just right-click the language where the data type (or interface) needs to be added and choose New . Then go
ahead and enter the data type (or interface) name.
For some reason, the C# Data Types folder doesn't include a DateTime data type in Visio. To
add it, follow these steps:
1. Right-click C# Data Types and choose New .
2. Enter the data type (or interface) name-in this case, it's DateTime , as shown in Figure
Figure 3. Datatype Properties window
Use Case Diagrams
Let's start with the first of the static UML diagrams, the use case diagram. In requirements analysis, uses cases describe the functionality of the system from the end user perspective, which identifies the system's boundaries and scope. They describe a sequence of actions initiated by external entities, either users or other systems called actors that provide some value to the project stakeholders or to other actor(s).
Basic elements in UML use case diagrams are as follows:
; Use case : This represents the use case itself, drawn as a horizontal ellipse.
; Actors : Actors are any external entity that makes use of the system being modeled,
including any person, device, or external system that have access to, or make use of,
the information and functions present in the system being modeled. They are drawn
as stick figures.
; Associations: These occur between actors and use cases, and are used when an
actor is involved with an interaction described in the use case. They are indicated by
solid lines with an optional arrowhead on one end of the line. The arrowhead is often
used to indicate the direction of the initial invocation of the relationship or to indicate
the primary actor within the use case.
To create a use case diagram with Visio, click the UML Use Case diagram stencil in the Shape
window to access the diagram's elements, and then click and drag elements to the drawing area as needed.
Continuing with the university system example, imagine I have a use case called Student
Enrolls in Seminar . To model it with Visio, I would do the following:
1. In the Model Explorer window, right-click Conceptual Model , select New --> Package , and
name the package Use Cases .
2. Right-click the New --> Use Case Diagram folder just created. Rename the diagram
Student Enrolls in Seminar . Notice how the Student Enrolls in Seminar use case diagram
also shows up in Model Explorer , as shown in Figure 4.
3. Click and drag an Actor from the stencil area to the drawing area. This actor is the
person (or system) that starts the action in the use case (the student in this example).
Double-click the figure to access its properties and rename it from actor1 to student .
Notice how the student actor also shows up in Model Explorer , as shown in Figure 4.
4. Click and drag a Use Case from the stencil to the drawing area. Double-click the ellipse
that represents the use case to access its properties and rename it from UseCase1 to
Enroll in Seminar . Notice how the Enroll in Seminar use case also shows up in Model
Explorer , as shown in Figure 4.
5. Connect the Student actor and the Enroll in Seminar use case by dragging an association
communicator from the stencil.
Figure 4. Use case diagram and Model Explorer window
Static Structure Diagrams: The Conceptual Model
Things start to get interesting here, since I'll use the most important diagram used for code generation, the static structure diagram, to represent the conceptual model diagram. Remember that the conceptual model won't include enough information to be transformed into code, but during the design process, you'll see how to work with static structure diagrams that you'll use when you're ready to work on the class diagram.
A conceptual model is a high-level static view of the objects and classes that make up the design/analysis space. It involves object-oriented programming (OOP) concepts such as classes, inheritance, and so on, which makes it the most (or one of the most) important UML diagram. It represents how the different objects in a model interact, without trying to define the objects' properties. The conceptual model only cares about understanding the problem that needs to be solved by identifying the main objects in the system and their relation. The conceptual model doesn't attempt to solve the problem, and therefore doesn't describe these objects and relations in detail.
Basic elements in UML static structure diagrams at the conceptual model level are as follows:
; Classes: Initially represented as boxes with three sections:
; The top section represents the name of the class.
; The middle section represents the attributes or properties of the class.
; The third section represents the methods of the class.
In the conceptual model, you aren't yet interested in attributes or methods, and classes will be represented by just a box. You'll see how this representation can be accomplished with Visio in a moment.
; Associations: Used to link different objects and represent static relationships
between classes. Most associations in UML diagrams are plain lines between classes
and/or objects. Every association can have a name and two ends to identify roles.
Roles are used to describe the nature of the association or the way the two classes see
each other. Each end's multiplicity can be set by a number or a range of numbers. You
will see different types of associations in detail later in this article, such as
; Generalization: (Used to represent inheritance.) A generalization is represented as
an inheritance link indicating one class is a superclass of the other, representing an "Is
a" relationship. It is represented with a triangle pointing to the superclass.
; Composition: (Used for showing part-whole relationships.) Composition is an
association in which one class belongs to a collection, representing a "Has a"
relationship. It is depicted with a diamond end pointing to the part containing the
Note: UML 2 (and Visio) no longer supports the concept of aggregation, a weaker form of composition, which was depicted in UML 1.x using a hollow diamond.
Visio creates by default an empty static structure diagram when you first start a UML model. To create a conceptual model diagram in Visio, follow these steps:
1. Click Static Structure under Conceptual Model in the Model Explorer window, and rename it
to something more meaningful, such as Conceptual Model Diagram .
2. Click and drag a Class element from the UML Static Structure to the drawing area and
rename it Student . Note that a Student class appears now in the Model Explorer window.
3. In the conceptual model, you are interested only in concepts or objects, not their
properties or attributes. To modify the appearance of a class, right-click the class and
select Shape Displays Options . In the Suppress section, check Attributes and Operations .
To make this change repeat automatically, also select the Apply to subsequently dropped
UML shapes of the same type in the current drawing window page check box. The class now
contains just the class name, like the Student class displayed in Figure 5.
Figure 5. Student class Display Options window
4. Add the additional classes that will compose the model: enrollment , seminar , course ,
professor , and address .
Next, I have to identify how the different classes in the conceptual model relate to each other by establishing associations.
Note: When connecting two classes with an association, make sure that both ends of the association are connected to a class through a class port (one of those light blue triangles that surround a class). If an association is correctly established between two classes, it will be displayed in black. If there is an error in one of the ends, the association will automatically display in red.
You can edit the properties of an association, such as composition, multiplicity, visibility, and so on, by double-clicking the association itself, as shown in Figure 6.
Figure 6. Association Properties window
Here is a brief explanation of the most important properties of an association:
; The IsNavigable check box defines whether to show the end of the association in code.
The end will be shown if the check box is checked.
; The Multiplicity of an association end is the number of possible instances of the class
associated with a single instance of the other end. Multiplicities are single numbers or
ranges of numbers.
; 0..1 means zero or one instance.
; 0..* or * means there's no limit on the number of instances.
; 1 means exactly one instance.
; 1..* means at least one instance.
Once the different associations have been established, I need to analyze the model, searching for generalization and composition. In the university system example, there are students, seminars, classes, professors, and addresses. Students and professors will share some properties. They both have an address, for example. Therefore, I design these classes as follows:
; Both Student and Professor are a generalization of a Person class.
; The Person class has an Address object.
Following the university system example, Figure 7 displays the conceptual model that depicts different objects that belong to the university system. Specifically, the model represents the students' enrollment in seminars that offer different courses taught by different professors.
Figure 7. Conceptual model diagram
Note: Remember that the conceptual model is based on a static structure diagram and could be therefore transformed into code by Visio. Because the information contained in the conceptual model is minimal, and you are still trying to understand the problem rather than writing the final solution, the code generated from this model would only be a general blueprint of the real code, with no practical use.
Organizing the Objects in the Diagram Using Layers
When you work with big UML diagrams, it can be very useful to use layers to identify different areas in the diagram and assign which objects in the model belong to which layer. Layers are used to organize related objects on a diagram. By assigning shapes to different layers, you can selectively view, print, color, and lock different categories of shapes. In the university system example in this article, for example, it might be useful to define objects in the conceptual model in two layers:
; Grades layer: Contains classes with grade-related information
; Personal layer: Contains students' personal information
Doing this allows me to work with the grade-related objects in the diagram without having the personal information-related objects interfering.