数据访问对象+业务对象+传输对象 - 简单示例

时间:2015-01-08 21:34:59

标签: design-patterns dao data-access-layer dto

我想了解abous DAO,Business Object,Transfer Objects和Assembler(Mapping)以及它们如何相互交互的概念。我使用这些概念做了一个简单的实现,并想知道它是否正确。

我将BO的持久性委托给DAO。是对的吗?或者更好的是使用DAO来获取TO然后在应用程序服务层中实例化BO? 如何创建TO< - > BO的汇编程序/映射? 使用数据库中的信息实例化新BO的正确方法是什么?

修改 看帖子:Should business objects be able to create their own DTOs?,我知道我正确的是使用DAO并检索TO。我将使用映射来创建我的BO。

但是看看这个链接:http://www.oracle.com/technetwork/java/dataaccessobject-138824.html,我想是正确的是BO使用DAO并检索TO。

我很困惑......

public interface IStudentDAO {

    public StudentTO getStudent(String name);
    public void save(StudentTO student);
}

public class StudentBO {

    private String name;
    private String course;
    private IStudentDAO dao;
    public StudentBO(String name, IStudentDAO dao){
        this.dao = dao;
        //How can i put an assembler here?
        StudentTO studentTO = dao.getStudent(name);
        this.name = studentTO.getName();
        this.course = studentTO.getCourse();
    }

    public void save(){
        //How can i put an assembler here?
        StudentTO to = new StudentTO(this.name, this.course);
        this.dao.save(to);
    }

}

public class StudentDB implements IStudentDAO{

    @Override
    public StudentTO getStudent(String name) {
        StudentTO s = new StudentTO(name, "dummy");
        return s;
    }

    @Override
    public void save(StudentTO student) {
        // persists StudentTO on database
    }

}

public class StudentTO {

    private String name;
    private String course;
    public StudentTO(String name, String course){
        this.name = name;
        this.course = course;
    }

    public String getName(){
        return this.name;
    }

    public String getCourse(){
        return this.course;
    }
}

0 个答案:

没有答案