Question:
How do I propagate constraints from Middle-tier to Client?
Answer:
This example uses the demos found in
\Delphi 3\Demos\DB\CLIENTDS\Empedit.dpr and \server.dpr.
Empedit.dpr represents the client tier, server.dpr runs on
the middle tier, and the database, DBDEMOS, is located also on
the middle tier, but in a real world scenario would be an SQL
database running on yet a third machine. This example uses the
Employee.db table. In order to use the following, you will need
to set up DCOM and have the two examples running before adding
the complication of constraints to the mix.
The general steps are:
Define constraints in the data dictionary
Associate the data dictionary attributes to a TField
Access the TField from a ClientDataSet.
First, define an attribute set in the data dictionary that
includes a constraint. For instance, an attribute set called
EmpNoA could have a CustomConstraint of "EmpNo > 100" and a
ConstraintErrorMessage of "EmpNo is less than 100". Define
static TFields in Server.dpr. Bring up the EmpServer datamodule
and double click EmpQuery to bring up the fields editor. In the
fields editor, right-click the EmpNo TField, select Associate
Attributes, and select EmpNoA from the Associate Attributes
dialog. Now when selecting the EmpNo TField in the fields
editor you should see CustomConstraint and
ConstraintErrorMessage matching the data dictionary entry. Open
EmpEdit.dpr and add a TDBEdit to EmployeeForm. Set the
DataField property to EmpNo and the DataSource property to
EmpData (the TClientDataSet). Compile and run Server.dpr on the
middle tier machine, then on the client machine run the EmpEdit
example application. Attempt to post a record where EmpNo is
greater than 100; your custom error message will appear.