Calculated field question

Calculated field question

Old forum URL: forums.lhotka.net/forums/t/771.aspx


NelsonF posted on Monday, July 31, 2006

I have a need to enter data for a record that contains a field calculated on the SQL server.
 
For example I have two fields ([apprvalue], [adjustment]) that calculate like this on the SQL server:
 
([apprvalue] + [adjustment])
 
The sum is then stored in the calculated field (not via trigger) named [adjustedvalue].
 
When a new CSLA object is created and I populate the [apprvalue] and [adjustment] fields, the [adjustedvalue] remains null since I havent commited my changes to the database. I understand this.
 
I need to get the adjusted value to use for further calculations in the parent CSLA object and this is where I am stuck. I can do the calculations myself but then I have to keep the calculation logic in two places, (the SQL server and the codebase). I could also commit the changes but then I have to commit all other changes to the parent and siblings as well and this might introduce problems because then I lose undo-ability (since I have to reload the data to get the new calc'ed values).
 
Has anyone run into these issues and/or have suggestions?

rkelley replied on Monday, July 31, 2006

From what I have found in my business objects is that it is typically much faster to store the logic in two places. It will take much less time to calculate the value in your business object than to save the data or call SQL to calculate the value.

What I am trying to do is very similar, reduce database calls and redundant logic. However, sometimes this is just not possible. What I have also done in the past is store variables for the calculation in the app.config so that the entire program does not have to recompile just for a value change. Or you could store the data in a table and load it into the app when it starts and just keep it in memory if it small amounts of data.

Hope this helps a little,

rkelley

Q Johnson replied on Monday, July 31, 2006

Perhaps ignoring the database version of the AdjustedValue is best.  You can just populate the property with a value in the Setter logic for its constituent parts (ApprValue and Adjustment).  If one of them changes, so does the calculation.  When one is undone, so is the other. 
 
You certainly don't need to write it to the database, why even read it?

Copyright (c) Marimer LLC