|
Comments
|
Today's Top SOA Links
Tools Great Data Grids I Have Known
And other tales from the code-generation front lines
By: Alan Fisher
Nov. 11, 2003 12:00 AM
Data table displays are the workhorses of transaction- based Web applications. So why are they so hard to build, especially since .NET provides a built-in ASP.NET DataGrid control? The reason is that, like all databound controls, data grid applications are not standard or necessarily predictable. Building them is so hard precisely because they demand custom treatment for each new application. Consider the difficulty of fulfilling each of these pretty standard requirements: The task of customizing data validators is one of those "creeping" tasks – while not difficult coding per se, it quickly becomes mind-numbing when you consider that every editable field on every page of your application has two to three relevant Validators that may require customization. In fact, one of the most enduring problems of writing user interfaces is figuring out how to display large amounts of custom data efficiently and intuitively without bewildering the user. The problem becomes particularly thorny when the interface must reflect hierarchical relationships within the data that the user needs to modify. However, while ASP.NET and Visual Studio .NET have certainly made Web application development simpler, efficiently creating intuitive, application-appropriate data grids still involves significant hand-coding and a mastery of the .NET Framework. And you must ask yourself whether investing a lot of effort in customizing DataGrid controls is really the best use of your time. The Code Generation Alternative Complex Displays The sample job application site shown in Figure 1 was generated using the Iron Speed Designer code generator with code generation tags inserted into an HTML layout page file. Each job application is a row in the data grid and includes two additional tables within the row – employment history and education. The code generation tags are illustrated in Figure 2.
![]() Figure 1 When you consider the difference in time and programming effort to insert one code generation tag into an HTML page as opposed to hand-coding all the ASPX, C#, or Visual Basic codebehinds – plus SQL statements for each control – it is easy to see how quickly a code generator saves you significant time. Some of the advanced functions enterprise-class code generators can handle include: Data Entry ASP.NET offers some shortcuts to make fields editable. However, making an entire data grid editable or allowing editing of template columns (using dropdown menus, for example), requires an advanced knowledge of ASP.NET. A state-of-theart code generator will allow you to make individual data grids or entire forms editable without hand coding (see Figure 3). Dropdown lists, radio buttons, and other controls should also be options for any data grid.
![]() Figure 3 Efficiently Managing Large Result Sets To minimize the data transmission load and the application's memory storage requirements, many application developers employ database cursor management – a conventional, wellestablished database technique – to manage the number of records downloaded from the database with each data grid page request. A code generator can generate cursor-management code that retrieves one page of data at a time, and only when requested, rather than retrieving the entire data set. Managing the "state" of your application pages without dragging down performance is one of the most difficult challenges for Web application developers. When cursor management is employed (see Figure 4), the application program doesn't attempt to maintain the data set. When the next page (or any other page) is requested, the generated application program simply passes the request to the database layer, where the appropriate SQL query is run again to request the needed page's data. The query parameters include the page number requested and the batch size, which is the number of records per page.
![]() Figure 4 Moreover, the SQL query is rerun so that any records added or deleted since the previous page fetch are properly accounted for. By contrast, if the cursor were simply maintained, any data updates would not be reflected when going from page to page. Custom Data Validation Note that additional validators may be needed for some fields. For example, if you set a limit of 11 characters for a phone number, you need to add a FieldValueTextLength Validator to ensure that an error message is generated if the phone number is not entered correctly. Full-featured code generators will generate these three required validators for each field, as well as others (like FieldValueTextLength) as needed. Let's look at a simple example. Note: This example uses Visual Basic .NET code, but the process is very similar if using C#. The real action happens in the RecordCustomControlValidator, where we customize and extend the default validation. This specific example extends the base RecordControlValidator for the "city" field to enforce a requirement that the city entered matches the shipping address for this record. In .NET, the base record control has two methods that allow custom validation: Override these methods to add your own custom logic. In an application generated by Iron Speed Designer, two classes are generated: the first is a "generated class" that is updated with each regeneration; and the second is a "safe class" that is never regenerated and preserves your custom logic. Example: Edit Customers Page - Validate city field Data Validation: In the City field validation example shown in Listing 1, we add additional logic to the "safe class" in order to override the validateData function. A safe class is generated once and never overwritten. Thus, it does not have to be rewritten for future applications if your code generator can adopt it and make it available for future development efforts After acquiring the City field, we compare it to "Mountain View". If the City is not Mountain View, then we return "False," generating an error message and keeping the user on the page. If the validator returns "True," the user proceeds to the next page. Note that the custom validation function is called every time the user clicks any button that requires validation. Thus, the "submit" button calls the custom control, runs the validation, and, if "True", continues the process. The "cancel" button returns "False" and aborts the process. This example is made simpler by the fact that it builds on generated code. Clearly, using a code generator to automate the composition of the several generated validators for each field (usually the three discussed earlier: RequiredField, FieldValue, and RecordControl Custom Validator) saves you significant amounts of work. The generated code can then be extended and modified for fields that require further customization, as illustrated in our City field customization example. When selecting a code generator, also make sure that your custom validations can be reused for subsequent applications, in order to leverage code assets across your entire organization. This Ain't Your Father's Code Generation In summary, code generation offers the following benefits: Getting advanced custom features "for free" from your code generator is more efficient than hand-coding ASP.NET from scratch. Make sure the code generator you choose (or build yourself ) achieves the level of performance and quality described in this article. Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||