When it comes to Aras best practices for server-side code there are two goals:
- Reduce the number of queries sent to the server.
- Reduce the volume of data retrieved from the server.
Applying the following best practices to your server-side code improves the consistency and efficiency of your server methods.
1. Use the Relationship Structure
One of the most common scenarios in Aras method code is to retrieve data from the parent or child items in a specific item’s relationship structure. For this example we will assume that we know an Activity item’s id and we need to find the related Workflow via the Activity’s parent Workflow Process.
One approach to finding the Workflow is to first query for the Activity’s related Workflow Process, then use the result to query for Workflows with an id matching the Workflow Process’ related_id. While the two-step approach shown below certainly works, it is not the best way to get the parent item because it queries the server twice to retrieve a single item.
The best way to retrieve data from related or parent items is to use the relationship structure to build a single query. The sample code below shows how to build the relationship structure that connects the Activity item to the Workflow we want to retrieve.
2. Use the “Select” Attribute
When querying the server for an item, we often receive more of that item’s data than we need to use in our method code. An AML query without a select statement will return the data you need, but it will also return a lot of data you don’t need.
For example, maybe we only really need the source_id and source_type properties from the parent Workflow we retrieved in the example above. If we execute the “best practice” query above with an Activity id, the result will look something like this:
However if we add a select attribute of “source_id,source_type”, the resulting AML will be a fraction of the size.
The following code shows our workflow retrieval query, modified to include a select attribute with a value of “source_id,source_type”.
The select attribute can also be used to select specific properties on an Item property. For example, below we use the select statement to retrieve only the name property from the Workflow’s related item.
3. Use the “idlist” Attribute
The idlist attribute helps us reduce the number of calls we need to make to the server. Instead of retrieving a collection of items with many queries, we can instead pass a comma-separated list of item ids on a single query.
In this scenario, we have an array of Workflow item ids and we want to retrieve all of the Workflows corresponding to those ids. While it may be tempting to loop through the array and retrieve each Workflow using getItemById(), this results in one query to the server for each id in the array. It will work, but you may begin to notice a change in performance with large data sets, especially if there are any onBeforeGet/onAfterGet server events on the ItemType.
Instead, we can retrieve all of the items in one query by using the idlist attribute. All we need to do is create a comma-separated string from our array of ids and set the idlist attribute on our query.
4. Avoid Using appendItem()
The simplest approach to building a query containing multiple items is to build and apply an AML string. While it is possible to use appendItem() to build a query, it’s slower and more cumbersome than simply appending AML to a string.
This example shows how to build a single query that adds multiple new CAD items to the database.
The following code shows how you might build a query using appendItem(). This approach works, but it also creates unnecessary items in memory and requires manipulating them via IOM functions.
The best approach is to build the AML via appending strings. This is quicker and requires only a single item to hold the results of the applied AML.
LOOKING FOR MORE ARAS INSPIRATION?
Stay tuned for future posts on Aras Best Practices.