Java / Spring: Beans to view

I have a dilemma, and I'm not sure of a better way to start solving it.

I work with the old code base at work. Some of the domain objects (and the db tables behind them) do not make much sense. for example, deletedstored as long, agestored as Stringetc. Which I was able to work with the beautiful. In the view, I can say if (deleted == 1).... But there is a certain business logic that leads to a service problem, having it in view. Here is one example:

String title = null;
if (obj.getTitle != null) {
    title = obj.getTitle();
} else {
    title = obj.getName() + " - " + obj.getCategory();
}

I would really like to have a β€œbean view” where this business logic and legitimate oddities are erased and stored, so I can use it in several views, and then change it in one place. If I had ProductPOJO and then mine ProductViewBean, I would do something like:

productViewBean.setDeleted( product.getDeleted() == 1 );
productViewBean.setTitle( product.getTitle() != null ? product.getTitle() : product.getName() + " - " + product.getCategory() );

My question is: where should I do this? Do I have to have manager(with the appropriate daosone entered into it) that is entered into mine controllerand returns my "view bean"? Or am I all wrong about this and could there be a better approach?

Thanks in advance

(: , , . . db/domain ( deleted boolean ..). - - (! title, " " ), , , , /. , - , .)

+3
2

, . . ProductViewBean , , getDelegate() dao.

public class ProductViewBean {
  private final Product delegate;

  public ProductViewBean(Product delegate) {
    this.delegate = delegate;
  }

  Product getDelegate() {
    return delegate;
  }

  public String getTitle() {
    if (delegate.getTitle == null) {
      return delegate.getName() + " - " + delegate.getCategory();
    }
    return delegate.getTitle();
  }

  public void setTitle(String title) {
    delegate.setTitle(title);
  }

  public boolean isDeleted() {
    return delegate.getDeleted() == 1L;
  }

  public void setDeleted(boolean deleted) {
    delegate.setDeleted(deleted ? 1L : 0L);
  }

  ...
}

, API, .

0

, API Spring .

, , -. - . , DAO.

+1

Source: https://habr.com/ru/post/1770934/


All Articles