1
\$\begingroup\$

I have ChannelWrapper and Channel Value Object. I want to convert each and every field of ChannelWrapper to Channel Value Object.

public class ChannelWrapper extends BaseWrapper implements APIWrapper<Channel>,
        APIUnwrapper<Channel> {

    @XmlElement
    private Long id;

    @XmlElement
    private User user;

    @XmlElement
    private Tenant tenant;

// Getter Setters
    @Override
    public Channel unwrap(HttpServletRequest arg0, ApplicationContext arg1) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void wrapDetails(Channel model, HttpServletRequest request) {

    this.id = model.getId();
    this.user = model.getUser();
    this.tenant = model.getTenant();

    }


    @Override
    public void wrapSummary(Channel model, HttpServletRequest request) {
    wrapDetails(model, request);

}

Channel Value Object:

public class Channel {

    private Long id;
    private User user;
    private Tenant tenant;
    //getter setters   }

Converter:

Channel channel = new Channel();
channel.setId(channelWrapper.getId());
channel.setUser(channelWrapper.getUser());
channel.setTenant(tenant);

Is this the best way to create a converter?

Any other general comments are appreciated.

\$\endgroup\$
1
  • \$\begingroup\$ I'm sorry but I don't get your point. You have a wrapper, a.k.a. Adapter, for a Channel, ChannelWrapper. Now you'd like to wrap this wrapper into another wrapper (or converter as you call it) such leading to a ChannelWrapperWrapper or ChannelWrapperAdapter. And you'd like to call it ChannelValueObject? Isn't that a bit confusing and/or over-engineered? \$\endgroup\$ – Gerold Broser Nov 26 '14 at 2:44
3
\$\begingroup\$

Since both classes have the same getters and setters this can most easily be achieved using apache commons beanutils:

Channel channel = new Channel();
BeanUtils.copyProperties(channel, channelWrapper);

if you have more complex requirements (e.g. different property names or types), I would recommend dozer.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.