In this step you build your first Bold UI, a form to create, read, update and delete customers. You will:
- Use TBoldListHandle to select objects from the object space
- Use TBoldGrid to show them in a table
- Use TBoldNavigator to create and delete objects
- Use TBoldEdit to view and edit a single attribute
- See how Bold keeps the controls in step with the objects
By the end, you have a working customer form with no SQL and little code.
- What a handle is and why you need one
- How an OCL expression selects the objects to show
- How
TBoldGridand the other controls bind to objects - How to save changes to the database
- How to handle dirty objects (unsaved changes)
A handle is a window into the object space. A list handle evaluates an OCL expression and holds the resulting list of objects:
Object space (holds the objects)
|
+-- TBoldListHandle (expression: Customer.allInstances)
|
+-- Holds: all Customer objects
Without a handle, you would write code like this:
var
Customers: TBoldObjectList;
I: Integer;
begin
Customers := dmInvoice.BoldSystemHandle1.System.ClassByExpressionName['Customer'];
for I := 0 to Customers.Count - 1 do
DisplayCustomer(Customers[I] as TCustomer);
end;and then write more code to refresh the display whenever a customer changes. With a handle, you set the expression and Bold does the rest:
lhaCustomers.Expression := 'Customer.allInstances';
// Bold evaluates the expression, tracks the selected customer,
// and tells the controls when the list or a customer changesHandles are the bridge between the object space and the controls on a form.
TBoldListHandle
RootHandle: BoldSystemHandle1 // where the expression starts: the object space
Expression: 'Customer.allInstances' // what to select
CurrentElement // the selected object (like a cursor)
When the system is active:
- Bold evaluates the expression on the root handle's value.
Customer.allInstancesloads all customers from the database the first time. - The handle holds the list of matching objects.
- The handle tracks a current element, the selected row.
- All controls connected to the handle stay in step with it.
A list handle points to one current object:
- When you click a row in the grid, the handle's
CurrentElementchanges. - All other controls connected to the handle (
TBoldEdit,TBoldLabel) show the new current object.
Example: edtName is a TBoldEdit connected to lhaCustomers with the expression name. It shows the name of
the current customer. Click another row, and edtName shows that customer's name. No code is needed.
The finished form (Step2Form.pas) looks like this:
┌──────────────────────────────────────────────┐
│ Step 2: Customer CRUD │
├──────────────────────────────────────────────┤
│ Grid of all customers │
│ (Name, City, Phone, Email) │
│ [|<] [<] [>] [>|] [+] [-] │
├──────────────────────────────────────────────┤
│ Name: [______________] │
│ Address: [______________] │
│ City: [______________] │
│ Phone: [______________] │
│ Email: [______________] │
├──────────────────────────────────────────────┤
│ [Save Changes] [Open/Close system] status │
└──────────────────────────────────────────────┘
Build it in your project from Step 1. Step2.dproj is the finished version to compare with.
First, add the data module's unit to the form's uses clause (File > Use Unit). Then the Object Inspector
offers the data module's components, such as BoldSystemHandle1.
Drop a TBoldListHandle (page Bold Handles) on the form and set:
Name:lhaCustomersRootHandle:BoldSystemHandle1of the data moduleExpression:Customer.allInstances
This tells Bold: "give me all Customer objects". The finished form also sets StaticSystemHandle to
BoldSystemHandle1; it gives the handle the model's type information at design time.
Drop a TBoldGrid (page Bold Controls) and set:
Name:grdCustomersBoldHandle:lhaCustomers
Right-click the grid and choose Create Default Columns: Bold adds a column for each attribute of Customer. With
Edit Columns you can remove columns or change them; each column has an OCL expression
(BoldProperties.Expression, for example name) and a title (Title.Caption). The finished form shows Name,
City, Phone and Email.
Drop a TBoldNavigator (page Bold Controls) and set:
Name:bnCustomersBoldHandle:lhaCustomers
The navigator has these buttons:
- First, Prior, Next, Last: move the current element
- Insert: creates a new Customer and adds it to the list
- Delete: deletes the selected customer, after asking (
DeleteQuestion)
Add a TBoldEdit (page Bold Controls) for each attribute you want to edit:
edtName: TBoldEdit
BoldHandle: lhaCustomers
BoldProperties.Expression: 'name'
edtCity: TBoldEdit
BoldHandle: lhaCustomers
BoldProperties.Expression: 'city'
edtEmail: TBoldEdit
BoldHandle: lhaCustomers
BoldProperties.Expression: 'email'
The finished form has one for each of Name, Address, City, Phone and Email. When you select a customer in the grid, the edit fields show that customer. When you type in a field, the change goes to the object in memory.
-
Add a Save Changes button. In its
OnClick:dmInvoice.BoldSystemHandle1.UpdateDatabase;
-
Add a button whose
ActionisBoldActivateSystemAction1of the data module, to open and close the system. -
In the form's
OnCreate, open the system as in Step 1:procedure TfrmStep2.FormCreate(Sender: TObject); begin dmInvoice.BoldActivateSystemAction1.OnSystemOpened := HandleSystemOpened; dmInvoice.BoldActivateSystemAction1.OnSystemClosed := HandleSystemClosed; dmInvoice.OpenSystem; end;
When you set the BoldHandle and the expression of a TBoldEdit:
- Initialization: the control asks the handle for the current object.
- Display: it evaluates its expression on that object and shows the value.
- Edit: when you type, it writes to the object in the object space, not to the database.
- Sync: when the handle's
CurrentElementchanges, all connected controls update.
This is data binding without data-binding code: no OnChange events, no manual refresh. Bold does it through its
subscription system.
Behind the scenes, Bold uses a subscription pattern:
- The
TBoldEditsubscribes to the object and attribute it shows. - When the attribute changes, the object notifies its subscribers.
- The
TBoldEditgets the notification and shows the new value.
So when code changes an object, every control that shows it follows:
Customer.Name := 'New Name'; // the grid and edtName show 'New Name' without any extra codeThe handle uses the expression Customer.allInstances:
- Customer: the class (like a table in SQL)
- allInstances: an OCL operation that means "all objects of this class"
It is roughly the SQL SELECT * FROM Customer. But instead of SQL you use the Object Constraint Language
(OCL), which is part of the UML standard. OCL works on objects, not tables:
Customer.allInstances → all Customer objects
invoices->size → the number of invoices of this customer
invoices->select(not isPaid) → this customer's unpaid invoices
Step 4 covers OCL in depth.
- Run the application.
- Click Insert in the navigator. A new, empty row appears in the grid.
- Type "Acme Corp" in the Name field.
- Click another row or another field. The new customer is in memory.
- Click Save Changes. The customer is now in the database.
Restart the application: the customer is still there. Bold loaded it from the database.
- Click a cell in the grid's Name column.
- Type a new value.
- The edit fields below show the new value.
- Click Save Changes.
- Select a customer.
- Click Delete in the navigator and confirm.
- The row disappears.
- Click Save Changes. The customer is deleted from the database.
Add a Discard button with this OnClick:
dmInvoice.BoldSystemHandle1.System.Discard;- Change a customer's name. Do not save.
- Click Discard.
- The grid shows the saved name again.
Discard throws away all unsaved changes in the object space.
Set the handle's expression to something invalid:
InvalidClass.allInstances
Bold reports an error when it evaluates the expression. Check an expression in the OCL editor of the Object Inspector before you use it.
The form asks before it closes with unsaved changes:
procedure TfrmStep2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := True;
if dmInvoice.BoldSystemHandle1.Active then
if dmInvoice.BoldSystemHandle1.System.DirtyObjects.Count > 0 then
case MessageDlg('There are dirty objects. Save them before exit?', mtConfirmation, [mbYes, mbNo, mbCancel], 0) of
mrYes: dmInvoice.BoldSystemHandle1.System.UpdateDatabase;
mrNo: dmInvoice.BoldSystemHandle1.System.Discard;
mrCancel: CanClose := False;
end;
end;DirtyObjectslists the objects with unsaved changes.- Yes saves them, No discards them, Cancel keeps the form open.
Every form of the tutorial does this: never lose unsaved work silently.
Changes in memory do not go to the database by themselves:
Customer.Name := 'New Name';
// The object space has the change, the database does not. You must call:
dmInvoice.BoldSystemHandle1.System.UpdateDatabase;If you forget, the changes are lost when the application closes.
A deleted object leaves Customer.allInstances, so the list gets shorter while you loop over it. Loop backwards:
// Right: iterate backwards
for I := lhaCustomers.ObjectList.Count - 1 downto 0 do
lhaCustomers.ObjectList[I].Delete;
// Wrong: iterate forwards while deleting
for I := 0 to lhaCustomers.ObjectList.Count - 1 do
lhaCustomers.ObjectList[I].Delete; // skips items, then runs past the endYou have a working customer UI. Customers without invoices are not much use, though.
Go to Step3_Invoices/README.md to add invoices and master-detail views.
Key Takeaways:
TBoldListHandleselects objects with an OCL expressionTBoldGridshows a list of objects; clicking a row changes the handle'sCurrentElementTBoldEditshows and edits one attribute of the current object- All controls stay in step through subscriptions
- Changes stay in memory until you call
UpdateDatabase - Check
DirtyObjectsbefore closing, so no data is lost