I would like to understand the concepts abous DAO, Business Object, Transfer Objects and Assembler (Mapping) and how they interact with each other. I made an simple implementation using these concepts, and would like to know if it is correct.
I delegate the persistance of the BO to the DAO. Is that right? Or Is is better to use the DAO to get TO and then instantiate the BO, in the application service layer? How can i create an assembler/mapping to TO<->BO? What is the right way to instantiate a new BO using information from database?
EDIT Looking at post: Should business objects be able to create their own DTOs?, i understand that i the correct is to use the DAO and retrieve an TO. The i will use a mapping to create my BO.
But looking this link: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html, i imagine the correct is the BO uses the DAO and retrieve the TO.
I am confused...
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;
}
}