| Using Visio 2000 Enterprise Edition to Model Applications
| Ken Spencer |
roper application design is a must in any project. Without this step, it is almost guaranteed that problems will occur. This is especially true in the component world. This month I am going to take a look at Microsoft® Visio® 2000 Enterprise Edition to see how it can be used to enhance the application design process.
Visio is a well-known drawing tool for creating ad-hoc drawings, floor plans, network diagrams, and almost any other type of diagram you need. This strong graphical heritage makes Visio a good choice for drawing software engineering diagrams. The keys to software engineering are supplying the proper drawing tools and providing integration with the coding environment or language. I'll take a look at both of these issues.
Visio includes a number of different diagram types that can be used to create software diagrams. Figure 1 provides a brief listing of the available options.
The variety of chart types make Visio a great candidate for projects that require many different types of charts. Visio can also generate database diagrams and schemas, further adding to the utility of this tool.
After carefully reviewing the Visio documentation, I found that the only model that seems to be able to generate code is the UML model. You can generate code from any UML model that contains class definitions for C++, the Java language, or Visual Basic®.
You can use the UML solution to reverse-engineer a project's class definitions, and then generate UML static structure models. This works only for Visual C++® 6.0, Visual J++® 6.0, or Visual Basic 6.0. To facilitate the integration between Visio and Visual Studio®, Visio installs a custom toolbar for Visual Studio if it's on your system when you install Visio.
Let's take a quick look at how Visio can be used with Visual Basic in the software design environment.
Starting a Software Project
The first step you must take in your application design process is to start Visio and select the UML model, as shown in Figure 2. Once you have selected the model, click OK. This will open Visio with a new model and the UML tools displayed. The UML Navigator explorer window can be found on the left side of the drawing (by default) and the drawing panel (including the UML stencil) will consume most of the window, as shown in Figure 3.
![Figure 2 Selecting Diagram Type](https://i-blog.csdnimg.cn/blog_migrate/bbdbf1d79bd60ce9b7cd3c8cba02c518.png) | Figure 2 Selecting Diagram Type |
The UML Navigator shows a tree view of your Visio project. You can interact with the UML Navigator to create new elements or change existing ones. Anything you do with a model's elements shows up in the UML Navigator, whether you modify it there or in a drawing. When you create a new model, the UML Navigator shows the default items, as shown in Figure 3.
Now that you have a model open, how can you design an application with it? First, think about what you are going to build. For this application, let's create Visual Basic-based components that handle the business logic. Before you start designing your model for the components, you should take care of a few settings.
First, you should select Code | Preferences from the UML menu. This will display the Code Generation Preferences dialog (see Figure 4). This dialog allows you to set a number of settings ranging from the directory that is used for generating code files to the actual settings for those files.
![Figure 4 Setting Initializers and Finalizers](https://i-blog.csdnimg.cn/blog_migrate/7074261330e42ad209fe50cd52297361.png) | Figure 4 Setting Initializers and Finalizers |
Select Visual Basic as the language to use. Next, select the folder for Visio to generate code into by entering the folder name in the Project Directory textbox. Note that you can also use the Browse button to select the folder. The folder must exist before you select it, but you can create a new folder with the context menu.
Using this dialog you can also change a number of settings that affect your generated code. The tree of options has two major folders. The Global Default Preferences folder contains all of the global preferences. The Element Pre-ferences folder contains specific settings for the project. As you add classes to a Visio project, they will become visible under the Element Preferences folder and you can explicitly choose particular settings for them.
For instance, you can select the Initializers and Finalizers under the Global Default Preferences folder and then turn on and off the creation of the Initialize and Terminate events in each class, as shown in Figure 4.
At this point, your project is set to use Visual Basic as its language. Now you can begin to work with the diagram. In the template, click the UML Static Structure header in the stencil to display the UML shapes for classes and key shapes you will need.
The first step you usually take in building components is to lay out the classes for a component and the interface (methods and properties) for each class. The UML representation for a class is the Class shape. You can add a class by dragging it from the UML Static Structure and dropping it on a diagram. The class then represents your class and will be used by Visio to generate the class module. The sample model shown in Figure 5 has two classes: Customer and CustomerDBService.
Figure 5 shows the classes and their interface. To create each class, I dropped a Class shape from the stencil into the diagram. Then I double-clicked the shape to set its properties. The Name property sets the class name (logically enough). Make sure the Visibility list shown at the bottom of the Code Generation Preferences dialog is set to Public in order to make the class available to the applications and objects that will use it.
The Operations tab of the Class properties dialog is used to create and set the names of the methods belonging to the class. To enter a method, click the New button, then double-click in the Operation field to set the default name and change it to the proper method name. Then select the return type for the method and select the visibility setting to control the visibility of the method. Figure 6 shows the settings for the methods in the Customer class.
![Figure 6 Method Settings](https://i-blog.csdnimg.cn/blog_migrate/4e30ed1bff620b207d9c3b268922b941.png) | Figure 6 Method Settings |
At this point, the methods do not have any arguments. To add arguments to a method, select the method in the Operation list, then click the Properties button. The Operation Properties dialog will be displayed next (see Figure 7). If you set a return datatype for the method, the method's name will appear in the list with the Kind column set to return. To add a parameter for the method, click New, then enter the name for the parameter and select its datatype. Figure 7 shows the dialog with the parameters for the RetrieveTitle method. After adding all of the properties, click OK to close the dialog. When you have added parameters for all of your methods, click OK to close the Class Properties dialog. Now your class diagram should show the class complete with its methods, parameters, and properties, as shown in Figure 5.
![Figure 7 Method Parameters](https://i-blog.csdnimg.cn/blog_migrate/a23d6292153bd5d63a9396ffba3101d1.png) | Figure 7 Method Parameters |
Now you can adorn the class with other features. For instance, you can draw links, add notes, or add any other information to the model by adding the shapes to the drawing and annotating them. The link between Customer and CustomerDBService in Figure 5 is a Generalization link. The properties for the link have the Stereotype set to Uses (indicated by the word uses on the link in Figure 5). Certain shape properties such as Link also control how the code is generated. If you do not set the Link properties to Uses, the generated code from Figure 5 will actually contain an Implements statement and implement the interface of CustomerDBService in Customer. Changing the stereotype property to Uses changes the generated code layout.
Another item you should notice is the folders in the UML Navigator. As Figure 5 shows, when you define an item using a shape in the model, it automatically shows up in the UML Navigator. Underneath the Static Model folder is the Package folder. In Figure 5, this folder is named CustomerPackage, which is the name that I entered for it. Underneath the CustomerPackage is the CustomerStructure, which contains the shapes for my classes. You can see the classes listed underneath the CustomerStructure. The package and structure folders are containers that you can use to organize your model. The package folder is important because its name is the name of the folder that Visio will use to place the class files it generates when you generate yours. Visio will automatically create the package folder underneath the path that you have entered in the preferences.
Now for the fun part. Once you have created your model and fine-tuned the classes, you can have Visio generate the classes for you. To generate the code, select Code from the UML menu, then select Generate. This will display the Code Generation Options dialog shown in Figure 8. You can select each of the classes you want to generate, or you can select all of the classes in a container by clicking the box next to the container containing the classes you want to create. Figure 8 shows the settings after I selected the CustomerPackage entry. In this dialog you can also change the path for the generated code and the path the code will be generated into. To actually generate the code, click OK and Visio will create the code files.
![Figure 8 Code Generation Options](https://i-blog.csdnimg.cn/blog_migrate/a6f65faef29c520fccc98698c6cc6284.png) | Figure 8 Code Generation Options |
When you are designing a class in Visual Basic, you must first consider the component it is tied to. You can tie your UML classes in Visio to a component shape in the diagram. When Visio generates the code for the classes, it does not use the component information with your generated code to create a Visual Basic project. Instead it simply adds the class files to the package directory under the path you entered. Once you have generated the class files, you must add them to your Visual Basic project from the Visual Basic editor.
You can also preview the code for a class without saving it to disk. To do so, select Code from the UML menu, and then click Preferences. Expand the Element Preferences folder, then select the class you want to preview. Now click the Preview button to display the code in the Preview Editor. Here, you can copy the code to the clipboard and then paste it into a module or other editor.
Conclusion
Visio is quite a powerful program. One of its many strengths is its ability to produce a wide number of drawings and diagrams. The many software diagrams that are available make it a good choice for producing software architecture diagrams.
The main drawback in using Visio instead of a specific software modeler is that Visio is still a general-purpose drawing tool that can also produce software diagrams. Specific modeling tools are designed to produce only software diagrams or possible software and database diagrams, and are therefore richer in those areas.
Using Visio to diagram component classes is a far cry from Web development, right? Not really. Today it is vitally important for every development project to contain some type of layout process. This allows members of the Web team to understand the overall architecture and flow of the site and make fundamental decisions about where the reusable code is, what the common services are, how data access is achieved, and so forth. Without this design step you are flying by the seat of your pants, so to speak.
|
|