title: Rich Domain Model and form mapping
body: <p>TLDR: How combine rich domain model with "heavy" setters with simple HTML form mapping?</p>
  
  <p>Problem occurs when setter of one property change other properties (directly or by using setters - this doesn't matter).</p>
  
  <p>Sample class for better explaining (in PHP, but this doesn't matter - I think):</p>
  
  <pre><code>class Product {
  
  private $name;
  private $cost;
  private $profit;
  private $price;
  
  public function getName() {
      return $this-&gt;name;
  }
  
  public function getCost() {
      return $this-&gt;cost;
  }
  
  public function getProfit() {
      return $this-&gt;profit;
  }
  
  public function getPrice() {
      return $this-&gt;price;
  }
  
  public function setName($name) {
      $this-&gt;name = $name;
      return $this;
  }
  
  public function setCost($cost) {
      if ($this-&gt;cost !== $cost) {
          $this-&gt;cost = $cost;
          $this-&gt;setPrice($this-&gt;getCost() + $this-&gt;getProfit());
      }
      return $this;
  }
  
  public function setProfit($profit) {
      if ($this-&gt;profit !== $profit) {
          $this-&gt;profit = $profit;
          $this-&gt;setPrice($this-&gt;getCost() + $this-&gt;getProfit());
      }
      return $this;
  }
  
  public function setPrice($price) {
      if ($this-&gt;price !== $price) {
          $this-&gt;price = $price;
          $this-&gt;setProfit($this-&gt;getPrice() - $this-&gt;getCost());
      }
      return $this;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The idea is this sample is that: you can set cost (of buying/creating product) and profit you want to earn - then the price (for selling product) get calculated. Also, you can set change the price - then profit is calculated.
  (Changing cost could trigger to change profit not price, but it doesn't matter for the problem).</p>
  
  <p>For example:</p>
  
  <pre><code>$foo = new Product();
  $foo-&gt;setName("Foo");
  $foo-&gt;setCost(100);
  $foo-&gt;setPrice(110);
  assert($foo-&gt;getProfit() == 10);
  $foo-&gt;setProfit(30);
  assert($foo-&gt;getPrice() == 130);
  </code></pre>
  
  <p>I need to have rich object in "backend" code due to many reasons (ie. command line frontend and CRON jobs).</p>
  
  <p>In HTML frontend you get form with 3 inputs corresponding to cost, profit and price. When user changes values and submit form order of setters does matters and can mess things up. For example:</p>
  
  <ul>
  <li>user goes click to edit product button</li>
  <li>sees form with values: cost=100, profit=10, price=110</li>
  <li>changes profit to 20</li>
  <li>submit form</li>
  </ul>
  
  <p><strong>And the problem is:</strong> Backend mapper gets values from POST form and calls: setCost(100), setProfit(20) and then setPrice(110) which is bad because it is old price which should be changed to 120 (user wanted to have 20 profit and 100 cost).</p>
  
  <p>Only solution i see is to implement <strong>all</strong> domain model logic also in frontend - in JavaScript - so changing cost/profit inputs trigger to change value in price input. Then all inputs have right values and are POSTed data can be put without "overriding" problem. It is easy with this sample but impossible when you have many fields (time consuming and error prone to write same code in two different languages) or really complex logic between them (not everything can be easy implemented in JS).</p>
  
  <p>Any ideas how to solve this? :)</p>
  
  <p>PS: Using transient properties and lazy evaluation (ie. only cost and price is property and editable, and profit is always calculated) is not an option. I must be able to change from both ways. Also it is in inconvenient for some heavy calculations - all props must be present and then saved to database.</p>