title: Java/Spring: Beans for the View
body: <p>I have a dilemma and I'm not sure about the best way to start solving it.</p>
  
  <p>I'm working with an old code base at work. Some of the domain objects (and db tables behind them) don't make a lot of sense. eg, <code>deleted</code> is stored as a <code>long</code>, <code>age</code> is stored as a <code>String</code>, etc. Which I've been able to work with fine. In the view I can say <code>if (deleted == 1)...</code>. But there are some specific business logic that is leading to a maintenance problem by having it in the view. Here's one example:</p>
  
  <pre><code>String title = null;
  if (obj.getTitle != null) {
      title = obj.getTitle();
  } else {
      title = obj.getName() + " - " + obj.getCategory();
  }
  </code></pre>
  
  <p>I would really like to have a "view bean" where this business logic and legit oddities are ironed out and stored so that I can use it in multiple views but then change it in one place. If I had a <code>Product</code> POJO and then my <code>ProductViewBean</code>, as an example, I would do something like:</p>
  
  <pre><code>productViewBean.setDeleted( product.getDeleted() == 1 );
  productViewBean.setTitle( product.getTitle() != null ? product.getTitle() : product.getName() + " - " + product.getCategory() );
  </code></pre>
  
  <p>My question is, where should I do this? Should I have a <code>manager</code> (with appropriate <code>daos</code> injected into it) that is injected into my <code>controller</code> and returns my "view bean"? Or am I going about this all wrong and could there be better approach?</p>
  
  <p>Thanks in advance</p>
  
  <p>(Note: I understand that the underlying structure is the real problem but it is beyond my jurisdiction to change at this point. Too many projects use these domain objects. And even if I did clean up the db/domain objects (so that <code>deleted</code> was a <code>boolean</code> etc) I'm still left with unavoidable business logic (if !title then 'build title from other components') that doesn't belong in the data layer and that I would like to encapsulate in one place so that neither the controller or view have to worry about it and it can be used across multiple controllers/views. I happen to be at a point where I can write something that is efficient and maintainable and could even create a good layer to ease cleanup of these domain objects in the future.)</p>