• No se han encontrado resultados

Revisión Externa Independiente

In document CODIGO DE SEGUROS DE SALUD DE PUERTO RICO (página 156-160)

The Microsoft Foundation Classes (MFC) are a set of C++ classes that provide a wrap-per around the Win32 API. To build a dialog box in MFC, you use the Resource Editor provided with the Microsoft Visual C++ IDE. You then build the C++ class to handle the dialog events using the Dialog Wizard. The Dialog Wizard creates classes, maps controls to classes, and handles some rudimentary validations. This wizard follows the mixed-code generation model by managing the C++ code within special comments. You are free to override the default behavior by adding custom C++ outside the comments.

5.6.1 Generator requirements

So why write another generator if Microsoft already has developed one? There are some drawbacks to the Dialog Wizard approach:

• The business details of the dialog box are mixed in with the code that maintains the state of the controls.

• The validations are fairly rudimentary, and there is no way to generate cross-field validations.

Swing generator

• You have no method of telling the Dialog Wizard how to read and write data to your API. For example, if your code is factored into a Model-View-Controller model, you can’t adjust the Dialog Wizard to fit that model.

Because the Dialog Wizard is usually very valuable, the generator should provide com-pelling value in these areas.

To separate the business details from the implementation details, first decide which business details are important to capture:

• The data source to get and set the data

• The fields and their mapping to the dialog resource

• Validations for each field

• Cross-validations between fields

• Custom requirements on a per-dialog or per-field basis

With this information, the generator should be able to handle these details:

• Building the MFC infrastructure to create the control objects and mapping them to the windows controls

• Building the data get and set code that initializes the controls properly and han-dles the OK button press

• Implementing the field and cross-field validations

• Implementing or importing the code for custom requirements

Clearly the case for building this generator is not as compelling as the Swing generator because of the excellent up-front work that Microsoft has done with the Resource Editor and the Dialog Wizard. However, there are some scenarios where you should make the investment in this generator:

• You are already generating a web interface for an application and you want a client/server interface. This generator could use your existing web interface defi-nitions to create the client/server dialog boxes.

• You are building an application with a large number of dialog boxes.

• The Dialog Wizard is not helping you because of your application data model or other specific requirements.

• You are not satisfied with the data exchange and data validation (DDX/DDV) model provided by the MFC and you cannot easily overcome those limitations within the MFC framework or by using C++ inheritance or templates.

5.6.2 Recommended tier generator architecture

The tier generator model shown in figure 5.11 creates MFC dialog classes directly from a set of business detail files written in XML.

TECHNIQUE: GENERATING MFC DIALOGBOXES 123 Using the business detail and custom code input files, the generator takes a set of tem-plates and creates the MFC dialog classes. The generator also validates the resource mappings in the business detail files against the resource (.rc) file.

5.6.3 Processing flow

The processing engine of the generator follows these steps:

Reads and validates the definition XML files and stores the information in local structures.

Checks for any custom code references. Reads in the code files and stores them in the local structures.

Reads in the resource file and stores the dialog boxes and their control IDs and types.

Checks the local structures against the dialog boxes and controls that were read from the resource file.

Merges the control type information in the .rc file with the definition file so you know which code templates to use to handle the control.

Iterates the following steps over every dialog box defined in the XML file:

Initializes code caches that will be used for the CPP and HPP files.

Iterates over each field and follows these steps:

Builds and caches the field definitions for the HPP file and the constructor code for the CPP file.

Builds the event handlers for the CPP file and adds the prototypes to the HPP method list.

Generator Definition Files

C++ MFC Dialogs Dialog

Class Table Dropdown Checkbox

Custom Code Files

Validations Event Handlers

Resource (.rc) file Application Logic

Figure 5.11 A generator that builds the C++ for MFC dialog boxes

MFC generator

Adds any custom per-field code to the CPP file and adds the prototypes to the HPP method list.

Handles any per-dialog custom code by adding the code onto the HPP and CPP code caches.

Uses the Dialog Class CPP template to create the implementation for the class.

This template should contain the class, the constructor, destructor, and message map. The code text from the field processing steps should be merged in. Stores the output of this template in the CPP file with the correct name.

Uses the Dialog Class HPP template to create the header for the class. This template should have the infrastructure methods (e.g., constructor and destructor). The template should then merge in the code created during the field processing. Stores the output of this template in the HPP file with the correct name.

5.6.4 Recommended mixed-code generation architecture

An alternative to tier generation for MFC classes is to use a mixed-code generation model on top of the mixed-code generation model already used by the Class Wizard. If your requirements are simple enough to fit into this model, then you will still be able to use the Class Wizard, which is not an option in the tier generator described previ-ously. Figure 5.12 shows the mixed-code generation model.

In this generator, you look for your own custom comments and augment them with custom code. As with the mixed-code generator shown in chapter 4, the gener-ator takes its input from the comments to generate the code for implementing the comment, and then replaces the existing comment with the original comment plus the new code.

Generator C++ MFC Dialogs

Dialog

Class Table Dropdown Checkbox

Validations Event Handlers

Resource (.rc) file Application Logic

Figure 5.12 A generator that builds MFC dialog boxes using the C++ dialog boxes in a manner similar to the Class Wizard

DESIGNTIPS 125 If your implementation only requires modification to the CPP or HPP file, you should consider integrating the generator into the Microsoft Studio Visual C++

MSVC IDE so that you can access it from the menu and have the generator work with standard I/O.

5.6.5 Processing flow

Here is a very basic process flow for this mixed-code generation model. You will have to adapt it to suit your requirements:

Reads in both the CPP and HPP files

Scans the files for your special comments

Performs these steps for each comment:

Parses any information required for generation from the comment

Generates the code based on the information in the comment using a template

Replaces the comment with the original comment text and the generated code

Creates backups of the CPP and HPP files

Replaces the CPP and HPP files with the next text

5.7 D

ESIGN TIPS

Here are some tips on how to design your solution up front so that you can generate large portions of the UI at implementation time.

• Try to break down your interface into three or four large types of interaction models. Database application HTML pages, for example, usually break up into table, form and query pages.

• Recognize that in most systems there will be more than a few exception cases that cannot be generated. The login page of a web application is a good example. In some cases, this means providing for custom code; in others, it means that the page will need to be handwritten.

• For HTML pages, design your pages as a set of nested components and contain-ers so that you can build consistency and quality through reuse.

• Again on HTML pages, work with designers and product management staff to keep the page flows simple and consistent. This will make the pages easy to gen-erate and to use.

• For dialog boxes, try to establish two or three basic layouts for tabs and controls.

• Also when building dialog boxes, you may want to use the nested template archi-tecture that was implemented in the case study in section 5.4 to create the popu-lar sand struts style of layout.

MFC generator mixed-code generator architecture

5.8 T

OOLS FOR GENERATING

UI

S

Here is a list of web sites where you can find tools available for purchase off the shelf to generate user interfaces:

• CodeCharge (www.codecharge.com/studio/) has an integrated development environment that can generate a single interface definition to multiple target environments (JSP, ASP, etc.).

• Visual Studio (http://msdn.microsoft.com/vstudio/) has interface tools and a Class Wizard that will write some of the Win32 dialog management code for you.

• X-Designer (www.ist.co.uk/xd/index.html) can generate code for a number of different platforms from a single definition.

• Proc-Blaster from Lockwood Software (www.lockwoodtech.com/index_pb.htm) uses template-based code generation, as we discussed in section 5.4.

5.9 S

UMMARY

Don’t fall prey to the idea that code generation will make your pages bland. Code gen-eration can build user interfaces that are of high quality, consistent, and elegant. Hand-tweaking tags and JSP code on hundreds of pages is wasted engineering time.

The user interface is easily one of the highest impact areas to use code generation.

Find a small portion of your project and use it to try one of the solutions in this chap-ter. You’ll see immediate productivity and quality improvements, and you’ll quickly be sold on this powerful technique.

In the next chapter, we’ll look at ways of using code generation techniques to create documentation for several types of applications.

127

C H A P T E R 6

In document CODIGO DE SEGUROS DE SALUD DE PUERTO RICO (página 156-160)