Hack 82. Generate UML Design Documents from Code

in Visio. Like all good developers,
you keep your design documentation up-to-date, right? Most likely,
that's a no. After all, who wants to constantly go
back and continuously update design documents while coding? Most
developers I know usually don't have the time
because they are too busy trying to get the application implemented.To help with this task, you can use Visual Studio to generate those
pesky design documents, in particular the class diagrams, by having it reverse
engineer your code. Although it won't replace and
update all of your design documents, it can cut down on the amount of
time it takes to maintain them manually. Essentially, this hack is
the opposite of [Hack #81].
10.7.1. Reverse Engineer Your Code
To get started, you first need to create a
Visual Studio Project. This can be any type of project, and most
likely it's one that you've been
working on or have already created. In this hack,
I'm going to use the
VehicleLibrary example found in
[Hack #81]. To refresh
your memory, the Solution Explorer of that project can be seen in
Figure 10-40.
Figure 10-40. VehicleLibrary project in the Solution Explorer

give it a name, and click Save.Depending on the size and complexity of your project, the reverse
engineering process may take a moment or two.Once your code has been successfully reverse engineered, an instance
of Visio is automatically started. You can see in the Model Explorer
that your classes have been automatically created (see Figure 10-41).
Figure 10-41. Classes automatically generated in Visio

Visio file has been added to your project under the Solution Items
folder.
10.7.2. Create Your Class Diagram
Unfortunately,
that's as far as the
reverse engineering will take you. You'll have to
create the class diagram manually. Depending on the size of your
class hierarchy, this may or may not be a big deal. The positive spin
to this is that Visio is smart enough to understand the relationships
between the generated classes.For instance, when you drag your classes onto a diagram, Visio
automatically determines their relationships and draws the
generalization lines for you. An example is shown in Figure 10-42.
Figure 10-42. Dragging classes onto a Visio diagram

structure diagram in Visio (see Figure 10-42), all
the arrowed lines were drawn for me. I didn't have
to remember all the relationships between the classes, which in this
case happen to be inheritance lines.So as you can see, Visual Studio can be used to reverse engineer your
classes from code into Visio for use on class diagrams, sequence
diagrams, and other related design documents. Dave Donaldson