Aras Best Practices: Server-side Code

When it comes to Aras best practices for server-side code there are two goals:

  1. Reduce the number of queries sent to the server.
  2. 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.

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.

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:

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”.

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.

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.

BEST PRACTICE

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.

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.

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.


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.

5 thoughts on “Aras Best Practices: Server-side Code

  1. Angela

    Helpful post with good examples!
    But what about a sequel? -> Aras Best Practices: Client-side Code

    While CSharp code in Aras is most of the time easy to understand, programming Javascript is always a big adventure (personal POV 😉 )

    Best regards
    Angela

  2. Eli Donahue Post author

    Hi Angela,

    We’ll be posting a blog on client-side best practices soon! Many of the server-side best practices also apply to client-side code (reduce the number of calls to the server, use select lists, etc), but there are certainly some tips specific to client-side code that we will cover.

    In the meantime, if you have specific questions about best practices or client-side coding in Aras, I recommend visiting the Aras development forum: http://community.aras.com/forums/forum/development/. Aras partners, community members, and the Aras Labs engineers share questions and expertise on a range of development topics there.

    Eli

  3. Angela

    Hi Eli,

    thanks for the feedback. I am looking forward for the sequel! Since I am currently programming a server-side method, I have a good additonal question for this topic:
    -> How far is the use of SQL queries in Methods recommended (direct queries, no procedures)?

    It is possible to use SQL for operations, which could also be done with AML-queries:
    One simple example:
    string mySQL = “DELETE FROM innovator.[” + targetTable + “]” ;
    mySQL += ” WHERE MY_PROPERTY = ‘” + thisID + “‘”;
    myResult = inn.applySQL(mySQL);

    Which variant is the winner here? AML or SQL?
    Most of the time I use SQL for complex queries, as I sometimes have trouble to get the specific data with AML.
    In this cases I often use direct queries inside of the code instead of procedures. But I am not sure if this is the best approach.

    Angela

Leave a Reply