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.
Incorrect
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.
gist.github.com/…/f0c770113ccb3237c69fb4e83c43601f
Best Practice
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.
gist.github.com/…/e60ee0bff052d1a3902aba33c7e5d466
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.
INCORRECT
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:
gist.github.com/…/fab3ef50c8104f1cd6a7b08307c4a67e
However if we add a select attribute of "source_id,source_type", the resulting AML will be a fraction of the size.
BEST PRACTICE
The following code shows our workflow retrieval query, modified to include a select attribute with a value of "source_id,source_type".
gist.github.com/…/a9fc08a295cdaaadb13ea387ca4030b2
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.
gist.github.com/…/b44250175979926102884402d4117a8c
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.
INCORRECT
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.
gist.github.com/…/be1aff5993a2cc82223082a75d5bbadf.js
BEST PRACTICEgist.github.com/…/be1aff5993a2cc82223082a75d5bbadf
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.
gist.github.com/…/c6595a6a14a77f375e8ad0f2537543bb
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.
INCORRECT
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.
gist.github.com/…/eeaae36fa7f35d459509f0c02b22be70
BEST PRACTICE
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.
gist.github.com/…/cb868d27bdffa94c0efa265d7ef601aa
LOOKING FOR MORE ARAS INSPIRATION?
Subscribe to our blog and follow @ArasLabs on Twitter for more helpful content! You can also find our latest open-source projects and sample code on the Aras Labs GitHub page.
Stay tuned for future posts on Aras Best Practices.