views:

30

answers:

1

All,

I'm developing an application using Zend Framework to manage tenders for construction work.

The tender will be a rather complex set of models with an architecture resembling the code snippet below.

My question is... should I store the total value of the tender as part of the tender model, or should I compute it each time it is required? The total value of the tender will be a sum of all the components (e.g. plant/labour/materials/overheads/etc).

object(Application_Model_Tender)#75 (4) {
  ["_id":protected] => int(1)
  ["_name":protected] => string(33) "Tender Name"
  ["_due":protected] => string(10) "2010-12-01"
  ["_labour":protected] => array(2) {
    [0] => object(Application_Model_Gang)#81 (3) {
      ["_id":protected] => int(1)
      ["_name":protected] => string(25) "First Gang Name"
      ["_gangMembers":protected] => array(2) {
        [0] => object(Application_Model_GangMember)#93 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(7) "Default"
          ["_cost":protected] => float(546)
          ["_qty":protected] => int(3)
        }
        [1] => object(Application_Model_GangMember)#91 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(8) "Custom 1"
          ["_cost":protected] => float(777)
          ["_qty":protected] => int(1)
        }
      }
    }
    [1] => object(Application_Model_Gang)#90 (3) {
      ["_id":protected] => int(2)
      ["_name":protected] => string(15) "Second Gang Name"
      ["_gangMembers":protected] => array(1) {
        [0] => object(Application_Model_GangMember)#92 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(8) "Custom 1"
          ["_cost":protected] => float(777)
          ["_qty":protected] => int(2)
        }
      }
    }
  }
}
A: 

I don't know zend specifically but as a general rule I'd recommend computing as the initial strategy. Store it only if you need to for performance reasons. Rationale:

  1. You need to write the caculation function in both cases.
  2. If you store (cache) it, you'll need a strategy for detecting changes to the tender structure & updating the cached value. That's additional complexity so only introduce it if/when you find you need it.

You should be able to hide the implementation strategy behind the interface [getTotal()] - so calling clients won't have to change if you decide to cache at a later date.

(There are of course caveats to above. If the structure is immutable then you can safely cache without the need for update detection).

sfinnie
That's good enough for me. I'll go for computing unless I hit performance issues.
Ben Muncey