WXLive for April 13 2012

On WXLive Peter Holemans presented Object Oriented Programming in WX, with a focus on developing a business and database I/O layer.

Peter lives in Belgium, is 41 years old and started in IT in 1996. He's mostly involved in large ERP contexts as an analyst programmer. He began using WinDev and1999 with version 5.5, has used WebDev since version 12 and WinDev Mobile since version 17.

Peter asked whether OOP is the right paradigm in WX. Back in 5.5 there was little OO support in the language, but there is much better support now. Whether you go in an OO direction depends on the suitability of the application architecture and on your knowledge of the OOP paradigm. It takes time to go from procedural coding to OO coding. 

OO code:

  • requires more analysis
  • at first sight may be less readable
  • causes you to lose some "10x faster" features in WinDev
  • has benefits such as encapsulation, inheritance, polymorphism and decoupling which add real value

Peter indicated that the purer your OO approach, the more you lose WinDev's RAD features, but you gain productivity advantages via OO as well. 

If you are looking at using the different Wx products together, if you want code reusability and the ability to expose business functionality to third parties, then OO is the way to go. 

Some standard classes

Peter has some standard classes he uses in all his projects. 

MyApplication

This is more or less a singleton,a nd handles INI files, database connections, login, email, exception handling notifications by email, and everything that's global.

ErrorMessage and ErrorList

These are used to pass error and warning messages between objects. 

Database entity classes

Methods include RecSave, RecAdd, RecModfy, RecDelete, RecGEtByID, Refresh, Validate.

Specialized classes

StringHandler, JDEHandler, etc. There may also be some specialized UI classes. 

Peter pointed out that it's very important to think in business terms not database terms.

Demo app

Peter ran an CRM app to indicate the kinds of things his applications frequently need to do. 

He returned to the data model and showed a phone book table, which he linked to his existing customer table. 

The next step is to write the queries. One thing to note is that defined queries are global, so you can't have two usages of that query at the same time (it's analogous to a shared file buffer in Clarion). 

Peter created two queries to begin with - one to retrieve a customer's phonebook entries and one to delete a customer's phonebook entries. 

He then created a class by copying one of his existing classes and making some changes. When creating a class to model the results of a query he sometimes just takes the query statement and adapts that to the class property declaration. Peter walked through the standard method code showing how he handles default values, validation and record updating. 

To plug his class into the UI he first declared an array of phone book objects, and then an instance of that array in his window. He created a table control with columns bound to the phone book list fields. He then executed his predefined query, used FiletoArray to copy the results of the query to the array, and then freed the query. Finally he called TableDisplay to show the results. Peter also showed how he updates the database with whatever changes have been made to the list.

The first hour covered the basics of creating a business object and wiring it into the UI; in the last half hour Peter enhanced the UI's use of the business class, among other things adding an update form and demonstrating the error messages shown when trying to save invalid data. 

The last half hour also demonstrated more clearly how in gaining OOP advantages you lose some of the RAD capabilities, since you're essentially hand coding the UI. 

Benefits for multi-platform development

This point came up later in the webinar. While the Wx product line lets you deliver to various platforms, if you are delivering the same business functionality to, say, desktop and mobile apps and you take the traditional approach, you're going to end up with a whole bunch of duplicated code. Taking an OO approach lets you reuse a lot of code. (And while Peter didn't have time to touch on testing, putting business logic into classes also makes that code much more easily testable.)

Remote databases

Peter wrapped up by talking about using HFSQL in the cloud, which means you have users who are accessing a database that is not on their local network. 

You can test the quality of the connection, so you can decide whether you want to use compression. If you have a slow connection you may want to use compression; if you have a fast connection then you have more overhead with compression/decompression.

Get data back in sets, rather than a record at a time. Use hExecuteQuery or hExecuteSQLQuery.

Ideally create your queries with the query editor rather than by writing the query in code. The predefined queries protect you against SQL injection and also lets WX optimize the query.

If you have single record requests use the h-commands.

If you have data that doesn't change much or at all load it at the start of the sesssion so you don't have to unnecessarily retrieve the same data again and again.

In WinDev, do not use tables/loopers directly linked to files/queries. Use a stateless web-page style approach.  

Download

Downloads are available at WxLive.us