30 Sept 2015

Calculating VO Transient Attributes

In this post I am going to consider a use-case when we need to calculate value of a VO transient attribute according to some complicated and resource-consuming business logic.

Let's say there is a method in a ViewRowIml class:
public String getValueForSomeTransAttr()
{   
  return getBusinessLogicFactory().
           getSuperComplexBusinessLogic.evaluateForKeyValue(getPrimaryKey());
}

We could use this method either in attribute's getter method:
  public String getSomeTransAttr()
  {   
    return getValueForSomeTransAttr();
  }

or we could use it in Groovy expression for the attribute:

Both options are bad, because the method is going to be invoked every time whenever the attribute value is accessed, and that could happen a number of times within the request. Of course we'd like to avoid extra executions of our super complicated business logic. Basically, the value of the transient attribute depends on primary key only, which means that we want to calculate it only once for each row (assuming that primary key value never changes). In order to meet this requirement we have to set "false" in Refresh Expression Value:


The same thing can be done using Edit Expression Editor:


Having done that, the method will be invoked only when a VO row is created from VO result set and when a new row is created by a user in UI.

That's it!




1 comment:

Post Comment