Ads 468x60px

Thursday, March 8, 2012

XAF : Business Classes vs Database Tables

In an application where you have to deal with information, you most likely have database tables, database systems, SQL queries, records and columns. Using relational models is a traditional way to work with databases. Today, the most common approach to database access is probably ADO.NET technology. It has the DataSet type that grants a local copy of multiple interrelated database tables. Via the DataSet object, a user can locally execute various operations, with the content of a database being physically disconnected from the DBMS.

The eXpressApp Framework (XAF) concentrates its efforts on another flow of today's development: object-oriented data handling. The need to store object-oriented data appeared when object-oriented programming became pervasive. Currently, most modern, nontrivial applications use an object-oriented paradigm to model application domains. This means that you can abstract from any persistence details, and have a clean, simple, object-oriented API to perform data storage. You do not need to handle persistence details and internal data representation in data stores be they relational, object-based, or something else. Why should you deal with low-level constructs of the data-store model, such as rows and columns, and constantly translate them back and forth? Instead, concentrate on complex applications you are required to deliver.

The ability to use object-based data handling, instead of direct interaction with databases, leads to a layer that provides mapping of objects into database tables (Object-Relational Mapping tool). In the XAF, such a layer is provided by the eXpress Persistent Objects (XPO). The XPO provides reliability and flexibility in storing the information. Its simplicity comes from the fact that you do not need to learn the details of how the data is stored.


With the XPO layer, to persist a class (provide its mapping) all that you need to do is to inherit it from the XPObject class. This class has the Oid property that uniquely specifies an object. In the XAF, the BaseObject class is used as a persistent class. It is inherited from the XPCustomObject class to satisfy the Framework's needs. XPO reads the content of a persistent class, and creates a database table for its read-write public fields and properties. By default, XPO gives the database table the same name as the class, and stores data in fields with the same name as the properties. You can easily override this behavior by using the Persistent attribute. The Persistent attribute can also be used to make XPO persist a private field, if required. If there is a property, class or field that you do not want to persist for any reason use the NonPersistent attribute.

To declare a persistent object:

   1:  <DefaultClassOptions()> _
   2:  Public Class Student
   3:     Inherits BaseObject
   4:     Public Sub New(ByVal session As Session)
   5:        MyBase.New(session)
   6:     End Sub
   7:     Private _firstName As String
   8:     Private _lastName As String
   9:     Public Property FirstName() As String
  10:        Get
  11:           Return _firstName
  12:        End Get
  13:        Set(ByVal value As String)
  14:           SetPropertyValue("FirstName", _firstName, value)
  15:        End Set
  16:     End Property
  17:     Public Property LastName() As String
  18:        Get
  19:           Return _lastName
  20:        End Get
  21:        Set(ByVal value As String)
  22:           SetPropertyValue("LastName", _lastName, value)
  23:        End Set
  24:     End Property
  25:  End Class
  26:   

The actual database is look like this:
OID
FirstName
LastName
1
Yaser
Aburrub
2
Mohammad
Yousef

Applying the DefaultClassOptions attribute to a class creates the following capabilities:
  • The Student item will be added to the main form's navigation control. When clicking this item, a list of the objects of the Student type will be displayed.
  • The Student item will be added to the submenu of the New toolbar button when objects of another type are displayed. Click this item to invoke a Student detail form and create a new Student object.
  • The Student objects will be provided as a data source to generate reports.
[eXpressApp Framework]

No comments:

Post a Comment