Compound Object
Introduction
This pattern lets you combine multiple objects to improve synchronization performance.
Challenge
Synchronizing data that is spread across multiple entities requires each entity to be synchronized individually. This can lead to performance problems because of the amount of data that is transmitted and the complexity of the queries on the offline database.
Solution
These performance problems can be countered by combining multiple objects into compound objects. It is then sufficient to synchronize only the compound objects and ignore much of the complexity of the server’s database on the client.
Implementation
To implement this pattern, do the following:
-
Create a new entity (for example OfflineProduct) to store the compound object.
-
Add all attributes needed in the offline client, including those from related entities, to the compound object.
-
Create a 1-1 association between the compound object and the target object. Configure the association to delete the compound object on deleting the target object:
-
Create a microflow (call it SUB_CreateOrRetrieveOfflineProduct) that retrieves and returns the compound object associated with the target object. If it does not exist, a new compound object is returned instead. For the new object, the association to the target object must be set:
-
Create a microflow (called ACO_Product, which will serve as the update microflow) and configure it as an After Commit event handler of the target entity. This microflow ensures that the compound object is created and updated when the target object is created or changed.
-
To do so, retrieve the compound object with the above microflow and set its attributes, retrieving all related objects as needed:
-
Create additional microflows as After Commit event handlers for all other entities that have attributes included in the compound object. These microflows ensure that the compound object is updated when the related object is changed:
-
Create a list of compound objects to commit all changes in a single action.
-
Traverse the associations with Retrieve by Association to get to the target entity. Use loops as needed.
-
Retrieve the compound object for the product and add it to the initially created list.
-
Change the compound object and update the attributes relating to the object from the microflow’s parameter.
-
Commit the list of compound objects.
-
Here is an example illustration for an entity that is nested 2 levels deep (Store → StoreSection → Product):
-
-
Replace all usages of the target object and related objects with the compound object in your offline client.
Recommendations
To improve your apps further, consult the following recommendations:
- The after commit event handlers used in the best practice can lead to performance issues if the target object or a related object changes frequently. In this case, use a designated update microflow instead of after commit event handlers.
- If associations to related objects can be empty, be sure to handle this in the update microflow — for example by using the expression
if ($Store=empty) then '-' else $Store/Name
. - It is often useful for compound objects to store aggregate values, such as the number of related objects. These can be computed using the appropriate List Aggregation action in the update microflow.
- It is assumed that compound objects are not changed by the offline client. If this is needed, combine the compound object with a Request Object.
- Combine the compound object with Incremental Synchronization to further increase synchronization performance.
Read More
- To increase the clarity, organization, and documentation of your apps, see Mendix Best Practices for Development