在Java中从一个对象转换为另一个对象的最佳/美妙方式

时间:2014-07-14 19:59:39

标签: java android

我有一个问题。

在我的Java App中我有2个POJO类型,一个是POJO类型,带有用于解析GSON的web服务的主体,另一个是我的逻辑模型对象,在这种情况下,对象是等于的,但是一个从实体扩展(Ada)框架,持久性)和webservice的对象不会。

这是一个例子:

这是一个webservice对象:

    package com.firext.android.domain.service;

/**
 * Created by usuario on 14/7/14.
 */
public class Replie {
    private Question question;
    private String reply;
    private int id;

    Replie(Question question, String reply, int id) {
        this.question = question;
        this.reply = reply;
        this.id = id;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public String getReply() {
        return reply;
    }

    public void setReply(String reply) {
        this.reply = reply;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

这与Persistence

的对象相同
public class Replie extends Entity {

    @TableField(name = "question", datatype = DATATYPE_ENTITY)
    private Question question;
    @TableField(name = "reply", datatype = DATATYPE_STRING)
    private String reply;
    @TableField(name = "id", datatype = DATATYPE_INTEGER)
    private int id;

    public Replie(Question question, String reply, int id) {
        this.question = question;
        this.reply = reply;
        this.id = id;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public String getReply() {
        return reply;
    }

    public void setReply(String reply) {
        this.reply = reply;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

我认为这是一个很好的做法,不使用相同的对象用于持久性模型和服务模型,因为我做到了。

然后我的问题是,我需要从服务对象转换为域/持久性对象,实际上我正在做这个(这是一个非常糟糕的方式)

private void saveWork(com.firext.android.domain.service.Work work) {
        try {
            com.firext.android.domain.Work newWork = new com.firext.android.domain.Work();
            newWork.setWsId(work.getAlertId());
            newWork.setTeam(Utils.getCurrentUserData(context).get("team"));
            newWork.setPassword(work.getPassword());
            newWork.setWorkString(work.getWorkText());
            newWork.setClientId(work.getIdClient());
            newWork.setAddress(work.getAddress());
            newWork.setTown(work.getTown());
            newWork.setProvince(work.getProvince());
            work.setPostalCode(work.getPostalCode());
            newWork.setPhone(work.getPhone());
            newWork.setDownloaded(true);
            newWork.setDate(new Date());
            newWork.setSignPath("");
            newWork.setPhotoList("");
            Report[] reports = new Report[work.getReports().length];
            int z = 0;
            for (com.firext.android.domain.service.Report report : work.getReports()) {
                int j = 0;
                ReplieGroup[] newRepliesGroup = new ReplieGroup[report.getReplyGroup().length];
                for (com.firext.android.domain.service.ReplieGroup replieGroup : report.getReplyGroup()) {
                    Replie[] replies = new Replie[replieGroup.getReplies().length];
                    int i = 0;
                    for (com.firext.android.domain.service.Replie replie : replieGroup.getReplies()) {
                        ReplieType replieType = new ReplieType(replie.getQuestion().getRepluType().getId(), replie.getQuestion().getRepluType().getName());
                        Question question = new Question(replie.getQuestion().getQuestion(), replie.getQuestion().getOrder(), replieType, replie.getQuestion().getId());
                        Replie newReplie = new Replie(question, replie.getReply(), replie.getId());
                        replies[i++] = newReplie;
                    }
                    ReplieGroup newReplieGroup = new ReplieGroup(replieGroup.getQuestionGroup(), replies, replieGroup.getId(), replieGroup.getName());
                    newRepliesGroup[j++] = newReplieGroup;
                }
                Report newReport = new Report(report.getId(), report.getName(), newRepliesGroup);
                reports[z++] = newReport;
            }
            newWork.setReports(reports);
            newWork.setStatus(Entity.STATUS_NEW);
            FirextApplication.getInstance().getDao().worksSet.save(newWork);
        } catch (AdaFrameworkException e) {
            ACRA.getErrorReporter().handleSilentException(e);
            isRunning = false;
            instance = null;
        }
        update();
    }

您可以看到我只将一个对象从一个包转换为另一个包。 我认为必须有任何模式或最好的方式来做到这一点......这不是很有效。

有人有推荐吗?

2 个答案:

答案 0 :(得分:2)

如果您将所有必需变量命名为相同并计划扩展类,我建议使用反射:

public void saveWork(Class whatever) throws NoSuchFieldException, SecurityException,     IllegalArgumentException, IllegalAccessException{
    for(java.lang.reflect.Field f : whatever.getClass().getFields()){
        if(this.getClass().getField(f.getName()) != null){
            this.getClass().getField(f.getName()).set(this, f.get(whatever));
        }
    }
}

这通常对我有用。

编辑: 这段代码需要一个类,并将它的价值复制给自己。如果你想复制,你可以直接替换"这个"与任何类和返回它或类似的东西:)

答案 1 :(得分:0)

Dozer库正是用于此类事情。它会将一种类型的bean映射到另一种类型的bean。因为您的属性在两个bean中都具有相同的名称,所以它不需要很多配置,但映射是完全可配置的。

见这里:http://dozer.sourceforge.net/