High-Performance Master-Detail CRUD with BatchDataObjects and .NET DataStore

Alex Riofrio
CODE AUTHOR
Posts: 2
 5 days 6 minutes ago #728 by Alex Riofrio
Alex Riofrio created the code: High-Performance Master-Detail CRUD with BatchDataObjects and .NET DataStore

How to execute atomic Master-Detail operations, generate client-side GUIDs, and minimize HTTP round-trips in PowerBuilder 2025 and later.

 

1. Introduction

Master-Detail data maintenance—such as Purchase Orders and their corresponding line items—is one of the most common patterns in enterprise applications. While traditional Client/Server architectures in PowerBuilder handle these operations seamlessly over a persistent database connection, migrating to modern stateless Web API architectures introduces new challenges.

When working with RESTful services, executing separate HTTP calls to save a header, insert or update multiple detail rows, and then refresh a master list increases overall response time due to multiple network round trips. Furthermore, splitting these operations across multiple requests compromises data integrity: if a detail update fails after the header has already been committed, the database may be left in an inconsistent state.

To solve this, PowerBuilder 2025 —combined with the .NET DataStore Web API—provides the BatchDataObjects object. This demo showcases how to build a clean, high-performance Master-Detail maintenance module that groups multiple DataWindow updates and retrievals into a single atomic HTTP transaction, leveraging client-side GUID generation via the Windows API.

 

 

2. The BatchDataObjects & .NET DataStore Approach

The philosophy behind this implementation is straightforward: prepare the Master-Detail data on the client side, transmit it in a single HTTP round-trip, and execute all database operations within a single atomic transaction on the server.

Instead of managing sequential REST requests manually, the .NET DataStore and BatchDataObjects approach provides:

  • Single Round-Trip Execution: Using RestClient.RetrieveOrUpdateWithModels(), multiple DataWindows (updates and retrievals alike) are serialized and sent together in one request, dramatically reducing network overhead.
  • Transactional Integrity (Atomicity): On the .NET Web API side, all changes encapsulated in the batch are processed within a single database transaction. If any operation fails, the entire batch is rolled back automatically.
  • Synchronized UI Refresh: Update operations (header and details) can be combined with retrieve operations (refreshing the main list) in the exact same call, ensuring the client always reflects the latest database state.
  • Client-Side Primary Keys with GUIDs: Traditional auto-increment (IDENTITY) columns force the client to save the header first, retrieve the generated ID from the server, and then assign it to the detail rows before saving them. By using GUIDs (Globally Unique Identifiers), the client generates a 128-bit unique key in memory before any database interaction occurs.
     

Why GUIDs Matter in Stateless Architectures

A GUID (UNIQUEIDENTIFIER in SQL Server) is a 128-bit identifier designed to provide practical uniqueness, even when generated by different computers or applications.

By generating GUIDs directly in PowerBuilder using the native Windows API (CoCreateGuid), the application can create a new Order header and immediately use its ID to link all related Detail rows, even before saving the data to the database.

This allows the entire Master-Detail data to be sent to the .NET DataStore API in a single request.

 

 

3. Solution Architecture & Key Components

The solution is structured across three main layers: a relational SQL Server database, a .NET 10 Web API powered by .NET DataStore, and a PowerBuilder 2025 R2 client application.

Database Layer (SQL Server)

To support client-side primary key generation both the Orders header table and the OrderDetail child table use:

UNIQUEIDENTIFIER keys:

sql

CREATE TABLE dbo.Orders (
    OrderID UNIQUEIDENTIFIER PRIMARY KEY,

....

CREATE TABLE OrderDetails (
  DetailID UNIQUEIDENTIFIER PRIMARY KEY,

....

 

Client-Side GUID Generator (Windows API)

Before inserting a new Order header or its detail rows, PowerBuilder generates a unique 128-bit identifier in memory by invoking the native Windows CoCreateGuid function from ole32.dll (or UuidCreate from Rpcrt4.dll):

powerbuilder

// External Function Declaration

FUNCTION Long CoCreateGuid(REF str_guid pguid) LIBRARY "ole32.dll"

 

Once formatted as a standard 36-character GUID string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx), this value is immediately assigned to dw_order_master and propagated as the foreign key (orderid) to every new row in dw_order_detail prior to calling the Web API.

 

Batch Orchestration (BatchDataObjects)

The core of the client-side data persistence relies on BatchDataObjects and RestClient.RetrieveOrUpdateWithModels(). Rather than executing separate requests for saving details, saving the header, and reloading the main list, all three operations are bundled into a single batch:

 

BatchDataObjects lbd_objs

RestClient lnv_RestClient

Integer li_rc

 

lnv_RestClient = CREATE RestClient

 

// Register updates respecting referential integrity, plus list retrieval

lbd_objs.AddUpdateObject(dw_order_master, True, True)

lbd_objs.AddUpdateObject(dw_order_detail, True, True)

lbd_objs.AddRetrieveObject(dw_orders_list)

 

li_rc = lnv_RestClient.RetrieveOrUpdateWithModels(lbd_objs, ls_api_url)

 

Critical Execution Rules:

  • Referential Integrity Order: When deleting a complete Order, all child rows in dw_order_detail are deleted first (DeleteRow()), followed by the parent row in dw_order_master. Registering dw_order_detail before dw_order_master in BatchDataObjects ensures SQL Server processes the child DELETE statements before the parent DELETE, avoiding foreign key constraint violations.


lbd_objs.AddUpdateObject(dw_order_detail, True, True)
//AcceptText = true, ResetUpdateFlags = true

lbd_objs.AddUpdateObject(dw_order_master, True, True)

 

  • Combined Update & Retrieve: Adding dw_orders_list via AddRetrieveObject() inside the same batch automatically refreshes the master list in the exact same HTTP round-trip once the transaction commits.
    When AddRetrieveObject(dw_orders_list) repopulates the orders list at the end of a batch execution, PowerBuilder synchronously triggers the RowFocusChanged event while RetrieveOrUpdateWithModels() is still finalizing its internal cleanup. Attempting to immediately retrieve dw_order_master and dw_order_detail synchronously inside RowFocusChanged can cause memory conflicts.

    To ensure clean execution, the order retrieval is decoupled using Post():

               Parent.Post wf_get_po(ls_po)

 
      By posting a custom user function, PowerBuilder completes the active BatchDataObjects lifecycle first and safely loads the 
      selected order's header and details milliseconds later.

 

 

4. The Demo Application

To demonstrate how BatchDataObjects and .NET DataStore work together in a real-world scenario, this demo provides a complete Master-Detail CRUD interface (.NET DataStore Master-Detail) built in PowerBuilder 2025 R2 and connected to a .NET 10 Web API.

 

 

The window is divided into two main panels with three synchronized DataWindows:

  • Left Panel — Orders List (dw_orders_list) & Global Actions:
    • Displays existing orders (OrderId, OrderDate, and CreateDate) sorted in descending order so the most recently created orders always appear at the top.
    • Selecting any row highlights the record in bright blue and triggers data retrieval to populate the right-hand Master-Detail panel.
    • Directly below the list, three icon-based action buttons allow the user to Create a New Order, Delete the Selected Order along with all its details, and Save Latest Changes in a single atomic batch transaction.
  • Right Panel (Top) — Order Header (dw_order_master):
    • Features a "Purcharse Order" displays the active order's read-only OrderId, an editable OrderDate date picker, and the read-only, server-generated CreateDate timestamp.
  • Right Panel (Bottom) — Order Details (dw_order_detail):
    • Displays the line items linked to the active order (Orderid, DetailId, Product, and Quantity) where both Orderid and DetailId are visually distinguished as read-only GUIDs.
    • Each row includes inline action buttons with descriptive tooltips (such as "Delete Detail") to remove individual line items, while the last row also displays an additional button to add new rows directly in the grid.

Running the Demo: Tips and Recommendations

  • Start the Web API First: Ensure the .NET 10 Web API project is running and verify that the target endpoint URL in the PowerBuilder client matches your active local or IIS port before executing operations.
  • Database Setup: Run the provided SQL script (SQLCreateTables.sql) to create the Orders and OrderDetail tables with UNIQUEIDENTIFIER keys and DATETIME2 default constraints prior to testing.
  • Inspect Batch Execution: Test creating a new order with multiple line items or deleting an existing order with several details to observe how all changes and the list refresh resolve in a single HTTP request.

     

5. Conclusion

Migrating Master-Detail maintenance modules from traditional Client/Server to modern Web API architectures doesn't have to mean sacrificing transactional integrity or increasing network traffic through multiple sequential HTTP requests.

By combining .NET DataStore, BatchDataObjects, and client-side GUID generation, PowerBuilder 2025 developers can encapsulate complex multi-table updates, deletions, and list refreshes into a single atomic round-trip. This approach keeps business logic clean, prevents partial-commit data corruption, and preserves the rapid development productivity that defines DataWindow-based applications.
 

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.