Click here to Skip to main content
15,881,715 members
Articles / Mobile Apps

Object Mapping For Hand-held Basic (HB++)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
21 Aug 2007CPOL2 min read 26.2K   61   6   1
Creating a Data-Access layer and Business objects for HB++

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.

Screenshot - ClassDiagram2.png
Figure 1. Class Diagram

So, if we want to use our classes in code, it would be something like this:

VB.NET
dim myAddress as AddressEntity

Private Sub Test()
Try
    'Create Address and save it
    set myAddress = new AddressEntity 
        
    myAddress.StreetAddress= "1 Short Street"
    myAddress.Suburb="Little Town"
    myAddress.Postcode="8900"
    myAddress.Address_Save       
        
    'Load it to check it out
    myAddress.Address_Load(1)
    msgbox(myAddress.StreetAddress & chr(10) & myAddress.Suburb)
        
    'Now Update it
    myAddress.Suburb = "Justa Town"
    myAddress.Address_Save 
        
    'Load it to check changes
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
I've been developing software for the last 15 years. Originally writing software in the medical field(Nuclear Medicine) and for the last 7 years commercial software. I worked for the last 7 years as subcontractor for several government and non-goveenment companies, mainly developing applications for the windows platform and the Palm OS/Compact Framework.

Comments and Discussions

 
GeneralBooks Pin
softpalm_wellington25-Mar-08 11:28
softpalm_wellington25-Mar-08 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.