In the case study presented in chapter 1, the user interface generator builds the JSP and Swing pages for the application. Figure 5.1 shows how the user interface generator fits into the whole of the generation system.
The user interface generator gets its input from a UI definition file and an API definition (which is built by the database access generator; see chapter 10).
Before we go any further, here are some terms we’ll use in this chapter:
• User interface—Either web pages and forms, or dialog boxes for desktop and cli- ent/server applications.
• Form definitions—The files used by the generator to define what fields and values should be presented to the user.
• Templates—Files used by the generator in the process of building the target files. These templates could be fragments of HTML or resource definition files, in the case of a Windows or Mac application, as well as fragments of production code to be used on the server or in the application itself.
• Usability—A measure of an application’s ease of use.
5.1.1 Why generate the user interface?
The major advantage of generating a user interface is that you can create a layer of
abstraction between the business rules for the interface and their implementation. User
interface code can be particularly messy, with lots of system calls that create a sea of Figure 5.1 The user interface generator from the chapter 1 case study
THEBIGPICTURE 101
minutiae within which lies some semblance of your business rules and the usability standards of your application.
The three-tier architecture clearly defines three components: the interface, business
logic, and storage. That may sound great on paper, but things are not always so clean
in implementation for several reasons:
• To have a reasonable user interface with client-side validation, you will need to have some of the business logic copied to the user interface tier.
• Some fields require cross-field validation. You must account for this in both the business logic and the interface layer. An easy solution is to submit a record to the business logic layer, and if there is a conflict, the form is re-sent by the server with error information. Usability would be improved if the form were validated before submission. Because this means hand-coding the cross-validation rules in two places (the user interface and the business logic layer), it is not often done. Code generation makes this enhanced usability possible by building both layers simultaneously.
• Removing hyperlinks from the pages of a web application that link to pages that the user cannot access for security reasons makes for an easy-to-use application, but it also means that some of the security information will have to migrate its way to the user interface.
Let’s take our accounting application as an example. The business rules of the applica- tion say that any Person object within the system that has selected external vendor must specify the ID of that vendor. Certainly rules like this one are codified in the business layer, but at the interface layer such a rule has to be hand-coded. This hand- coding creates possible synchronization problems because the front-end field valida- tions can get out of sync with their back-end business rule counterparts.
Another common solution is to send the possibly errant record to the business logic layer for validation. This is a clean implementation, but the usability suffers because the user may be presented with a cryptic message and returned to the form. Using code generation, you can specify the security and validation parameters for each object in one definition and have the generators use that definition when creating the data access, business logic, and user interface layers. Changing a security or vali- dation parameter in the definition file modifies every tier of the application so that the new rule is consistent throughout.
In summary, here are some of the key benefits of user interface generation: • Consistency—Generating forms en masse creates forms that are consistent and
that present uniform usability standards to the user.
• Flexibility—Using a tool to build your interfaces means that you can respond to requirement changes quickly. Security changes underscore this flexibility; if you change and enhance your security API you can alter the templates to match, regenerate, and have an interface that conforms to the new security standards.
• Portability—As good as some of today’s user interface toolkits (e.g., Qt or Tk) are, they still bind the business logic of the forms to the implementation details. Having a high-level representation of the business requirements of the forms in your application allows you to retarget the generator to any number of different interface technologies. For example, from one form definition you should be able to generate both web and client/server interfaces.
5.1.2 Integration with existing tools and other generators
Many high-quality tools are available for building web pages and dialog boxes for desk- top applications. Are we suggesting that you scrap those tools? Of course not.
Say you’re using Microsoft’s Visual Studio to build desktop applications. Visual Studio’s dialog editor creates text-based dialog resource files that a generator can read. The generator can then check the controls specified by the business logic layer against the controls on the dialog box.
To increase your overall productivity, we recommend you continue using existing tools while building generators that work in conjunction with those tools.
While the UI is the first thing the customer encounters, it is the end of the line when it comes to implementation. This makes sense because you must implement the interfaces the UI talks to before implementing the UI itself. The case study in chapter 1 showed a user interface generator working in conjunction with the back-end generator. This is a powerful architecture because it reduces redundant data entry and ensures the user interface layer has information about the back end so that it is able to provide a high-quality user experience. Interface generators can gain by pairing with the back-end generator if there is one.