Data-Aware Business Components
At a high-level, data-aware business components allow Delphi developers to encapsulate
business rules inside a component definition rather than scattered about inside form event
handlers. By encapsulating the business rules within a component, the business rules
become isolated from the User Interface layer. Furthermore, implemented as Delphi
components, business components are easily reused in separate applications. Simply drop
the appropriate business component on the form and connect it to a dataset. All of the
business rules associated with that object are available through the components
properties and methods.
At a low-level, data-aware business components are nonvisual Delphi components that
descend from the TRzBusinessComponent abstract base class. The data-aware business
component architecture relies on field objects to provide access to the actual data. As a
result, data-aware business components do not sacrifice the data-aware controls that come
with Delphi. In fact, business components work seamlessly within the Delphi development
environment, both at design-time and at run-time.
For More Information...
Chapter 13 in Developing Custom Delphi 3 Components
covers the entire data-aware business component architecture. Including the base classes
provided by the architecture, and more importantly, how to construct your own business
components. In addition, this chapter presents a very detailed description of the purpose
and philosophy behind business components. The following paragraphs summarize the
Overview of the Architecture
The key to this architecture is the TRzBusinessComponent class. Actual business
components are defined as descendants of this abstract base class. Business components
maintain a set of field object references that are linked to a particular dataset.
Whenever the business component needs to access data, it uses the field objects. By using
the field objects, the business component is automatically aware of any changes to data
made by data-aware controls, including grids.
However, business components don't just use field objects, but rather they are able to
explicitly create field objects without having to use the Fields Editor. This enables
business components to work seamlessly within the Delphi environment.
Every business component inherits a Dataset property from the TRzBusinessComponent
class. This property is linked to the desired dataset either at design-time or at
run-time. When the business component is connected to a dataset at design-time, the
business component explicitly creates the desired field objects using the same mechanism
used by the Fields Editor. This even results in field objects appearing in the forms
class declaration. More importantly, the business component itself obtains a reference to
each of the field objects, which it can then use to support business rules and functions.
Although the TRzBusinessComponent can be used in isolation, it is much more effective
when used with either a TRzBusinessTable or TRzBusinessQuery. These two classes are simple
descendants of the standard TTable and TQuery components. Their impact on the architecture
comes from their ability to communicate with a business component. When the Dataset
property of a business component is connected to a TRzBusinessTable or TRzBusinessQuery,
the business component is able to intercept the events of the dataset.
For example, validation is probably the most important utilization of business rules.
Typically, validation is performed during the BeforePost event of the corresponding
dataset. The new table and query components define an OnStartPost event which is only
visible to the TRzBusinessComponent class. When the dataset record is about to be posted,
the business component intercepts the BeforePost event and generates the OnStartPost event
for the business component. The business component responds to this event by calling its
virtual Validate method. Therefore, the Validate method is guaranteed to be called before
a record is posted. In addition, the end-user is still able to write a custom BeforePost
Handling Validation Errors
The Raize Business Component Architecture utilizes the TRzErrorHandler component during
validation of a data-aware business component. By simply attaching an error handler to a
business component (via the ErrorHandler property), the error handler will be used to
display validation errors and validation warnings to the user.
To provide custom validation in a business component, override the Validate method.
Within the method, use the field object references of the component to test the contents
of the current record. If invalid data is detected, use the Add method of the ErrorHandler
to record the validation error or warning. This method takes a message string and an error
type. For validation, the two important types are etWarning and etValidation.
Displaying the error dialog box is handled automatically by the business component.
However, the dialog box will not be displayed if there are no validation errors or
warnings. When validation is completed, the business component checks the ErrorHandler's
error list. If it is empty, the record is posted. If not, the error handler dialog box is
displayed. The dialog box displays all validation errors and warnings in a list
box. If the list contains only warnings, a Proceed and Cancel button are displayed. If the
list contains an error, regardless of whether it contains any warnings, an OK button is
Other Methods of Interest
In addition to Validate, there are two other methods of interest. Override the
InitFields method to specify initial values to be used when a new record is created.
Likewise, override the CalculateFields method to have the business component determine
calculate field values. It is important to realize that both of these methods along with
Validate eliminate the need to provide equivalent code in event handlers on the form.
Furthermore, the CalculateFields method, being part of a business component, actually gets
called at design-time. This results in calculated field values being calculated at
Download the Source Code
All of the methods described on this page are demonstrated in the TRzDBEmployee sample
component, which is included in the architecture. To download the latest version of the
Raize Business Component Architecture, proceed to the Download page
by clicking here.