Introduction
The article will show you how to do some basic object mapping of an HB++ database table. The whole process will allow you to create a simple data access layer (on single table) and two business objects, a business entity and a collection of this business entity.
Background
Object mapping has been around for a while now and you can find it applicable to almost any available computing platform. Although the first object mappers or O/R mapper were in Java©, you can certainly find an object mapper in almost any programming language. The basic principle of an object mapper is quite simple and it can be resumed, very broadly, in the following steps: Analyze database schema and retrieve information of tables, relationships, indices and so on, usually called metadata. Once the mapper has gathered all this information, it proceeds to create a few classes. Starting with the data-access classes (think of them as the interface between your database and your business classes) and then the business classes (think of these ones as objects that your application uses to perform all operations and tasks your application should perform).
Using the Code
The class diagram in figure 1 shows how the classes are implemented. The AddressDACL
class inherits from the Recordset
(in our case the Address
table) and therefore will inherit all the properties and methods from recordset
in addition to the methods our class may implement. In our case, AddressDALC
will implement the methods shown on the diagram. The AddressEntity
class represents the business object we will use in our application. It contains properties that map the fields in the table. It also contains some methods that allow to access the data layer, using AddressDALC
. The AddressCollection
class is a collection of Address
entities and provides all the methods required for a collection.
Figure 1. Class Diagram
So, if we want to use our classes in code, it would be something like this:
dim myAddress as AddressEntity
Private Sub Test()
Try
set myAddress = new AddressEntity
myAddress.StreetAddress= "1 Short Street"
myAddress.Suburb="Little Town"
myAddress.Postcode="8900"
myAddress.Address_Save
myAddress.Address_Load(1)
msgbox(myAddress.StreetAddress & chr(10) & myAddress.Suburb)
myAddress.Suburb = "Justa Town"
myAddress.Address_Save
myAddress.Address_Load(1)
msgbox(myAddress.StreetAddress & chr(10) & myAddress.Suburb)
Catch
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Test"
End Catch
End Sub
It can be seen that the sample code becomes a lot simpler and easier to read.
Points of Interest
The main advantage of using object mapping is that code becomes simple and a lot easier to maintain. As an application become larger and data access more complex, object mapping benefits become more apparent.
The main disadvantage of object mapping is also quite obvious: very similar code written time and again. Subclassing takes away most of the repetitive code but all DALC-type classes should implement the basic CRUD methods. Code generators, usually simplify the creation of classes using object mapping. There are many such applications for different platforms but unfortunately none for Palm OS©. My next article will focus on the design and implementation of such a tool, so we can apply object mapping to HB++ applications, automatically.
History
- 21st August, 2007: Initial post