Hack 81. Generate Code from UML

using Visio to generate your code. When Microsoft purchased
Visio for its business productivity division in 1999, the company
began working on getting it to better integrate with its Office and
Visual Studio product suites. This has lead to advancements in Visio
itself, especially as to the use of UML for designing applications.
UML is an industry standard that allows architects and developers to
produce design documents and diagrams that are commonly understood
and used during the development of an application.One of the advancements in Visio is the ability to generate code from
the design documents, in particular UML class diagrams that can be
used to generate code for your classes in Visual Studio. Visio can
generate the code in C#, VB, or C++, and you can even create your own
templates in Visio for how you want it to generate your code. For
application teams that utilize design documents, this can be a nice
jump start to your coding effort.
|
10.6.1. A Simple Class Diagram
To have
Visio generate code for your classes, you first need to create a UML
class diagram. To create new diagrams, navigate to File
New
shapes in the UML Static Structure section to create your class
diagram. Figure 10-34 shows the class diagram I have
created to generate code from.
Figure 10-34. Class diagram of Vehicles class hierarchy

Vehicle class hierarchy. This rather simple example shows an abstract
base class named Vehicle that contains a few properties (Make, Model,
Year) and methods (Accelerate, Decelerate, Drive, Start, Stop). The
class diagram then shows other implementation classes and their
relationships to one another.
10.6.2. Generate the Code
Now
that you've created a
class diagram in Visio, follow these steps to autogenerate the code
in Visual Studio:On the File menu, click UML
Generate dialog box will appear.For Target Language, select C# (or C++ or Visual Basic).Type a Project Name and select a Location to put it in.Check the "Add classes to Visual Studio
Project" checkbox.Select the template to use. The template list contains several Visual
Studio Project types to choose from.Type a Solution Name and check the Create Directory for Solution
checkbox.Select all classes for which you want to generate code.Click OK. A sample Generate dialog is shown in Figure 10-35.
Figure 10-35. The Generate dialog

10.6.3. View the Code
Now
that you've
generated your class code from Visio, go
take a look at it. Go to the location that you chose in the preceding
steps. You should see a folder with the name of your solution. The
solution folder will contain a Visual Studio Solution file as well as
a folder with the project name you specified. For instance, in this
example, the solution is named GenerateCodeFromUML and the project is
named VehicleLibrary.Double-click your generated solution file to open a new instance of
Visual Studio. Chances are the IDE will show the
Class1.cs file in the editor. But wait, you say,
I didn't create a class named
Class1 in Visio. Right you are. The best I can
figure is that this is a small quirk with Visio's
code generator. You can either ignore this file or just delete it.The important thing is that you will see a file for each of your
classes in the Solution Explorer, as shown in Figure 10-36.
Figure 10-36. Generated class files in the Solution Explorer

you can now begin to modify it as needed. But first, take a look at
the generated code. Here is the code for the abstract base class
Vehicle:
// Static ModelAs you can see, Visio does an adequate job of generating code for
public abstract class Vehicle
{
public string Make;
public string Model;
public string Year;
public virtual void Accelerate( )
{
}
public virtual void Decelerate( )
{
}
public virtual void Drive( )
{
}
public virtual void Start( )
{
}
public virtual void Stop( )
{
}
}// END CLASS DEFINITION Vehicle
your classes and can save you an immense amount of time, especially
if you have a class library with a lot of classes that contain a lot
of properties.
10.6.4. Hacking the Hack
You've seen that Visio does a pretty decent job of
generating your class code right out of the box. But what if you
didn't quite like the way it generated your code? Or
maybe its format is a little off from your own way of writing code.
You can edit and/or create your own code generating templates so that Visio generates code
that way you like it.To get to the code template editor, click UML
Several options are found in this dialog, but for the purposes of
this hack, I focus on creating two new templates, one for your own
class template and another for your own property template.
10.6.4.1 Create a new class template
With
the
Preferences dialog, open,
under Default, click Code Templates. This will show additional
options, as shown in Figure 10-37.
Figure 10-37. The Preferences dialog with Code Templates selected.

ClassImpl1. The safe way to create your own new
template is to copy ClassImpl1 and then modify it.
To do this:Under Categories, select ClassImpl. A template
named ClassImpl1 appears in the Templates list.Click Duplicate. The Edit Template window will appear (Figure 10-38). I won't go into all the
details here, but what the Edit Template really shows is a
combination of normal text and Visio
macros. The macros are denoted by
the %macro_name% format. There are too many macros
to cover here, but you can see the list by opening the Visio help and
reading the help topic titled "Use built-in macros
to speed up code formatting".Give the new template a name and modify the template as you see fit.Then click OK. Your template name will now appear in the Templates
list.Under Default, click Class.Under Class Template, select your class template name from the
Implementation list. Click OK.Now every time Visio generates class files for you, it will use your
new class template instead of the default one.
Figure 10-38. Edit Template window

10.6.4.2 Create a new property (attribute) template
By default,
Visio creates code for
properties in this format:
public bool CargoNet;However, your coding style might have properties looking like this:
private bool m_CargoNet;To create your own template for properties (called attributes in
public bool CargoNet
{
get { return m_CargoNet; }
set { m_CargoNet = value; }
}
Visio), follow these steps:With the Preferences dialog open, under Default, click Code Templates.Under Categories, select Attribute.A template named Attribute1 will appear in the
templates list. Click Duplicate.The Edit Template window will appear again. For our example, make the
template look like Figure 10-39.Click OK. Your template name will now appear in the Templates list.Under Default, click Attributes.Select your attribute template from the Templates list.Click OK. Now when Visio goes to generate your property code, it will
use your template instead of the default.
Figure 10-39. New Visio code template for properties

10.6.4.3 Regenerate your code and view it
Now
that you've created your
own code templates for classes and properties, regenerate your code
by following the steps just listed. Then open your generated solution
and open the Minivan.cs file in the Visual
Studio editor. Notice the format of the properties has been updated
to reflect the use of your code template:
// Static ModelI've demonstrated a simple example of using Visio to
public class Minivan : Van
{
private bool m_CargoNet;
public bool CargoNet
{
get { return m_CargoNet; }
set { m_CargoNet = value; }
}
private bool m_DualSlidingDoors;
public bool DualSlidingDoors
{
get { return m_DualSlidingDoors; }
set { m_DualSlidingDoors = value; }
}
}
// END CLASS DEFINITION Minivan
autogenerate your class code to get a jump start on your coding
effort. However, Visio code templates can be customized much further,
and I suggest you experiment with it to find the right degree of code
generation you are comfortable with.