June 1, 2011, 3:05 p.m.
posted by fullstack
Generate UML Design Documents from Code
Use Visual Studio to generate design documents 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].
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.
VehicleLibrary project in the Solution Explorer
To reverse engineer the code for this project, do the following:
Click Project Visio UML Reverse Engineer.
Browse to the location where you want your Visio file to be saved, 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).
Classes automatically generated in Visio
You can also see in your Visual Studio instance that the generated Visio file has been added to your project under the Solution Items folder.
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.
Dragging classes onto a Visio diagram
As I was dragging classes from the Model Explorer onto a static structure diagram in Visio (see Figure), 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.