I know this is probably a centuries old question, but what is the best practice? Using a domain model object at all levels of your application and even binding values directly to them in JSP (I use JSF). Or convert a domain model object to a DTO at the DAO or Service level and send a lightweight DTO to the presentation level.
I was told that it makes no sense to use DTO, because changes to the database will lead to changes to all of your DTOs, whereas using model objects everywhere will require changes to the affected model object. However, the ease of use and the lightweight nature of the DTO seem to outweigh this.
I should note that my application uses Hibernate Model Objects and uses its own user-created model objects (which is not associated with any database session, it is always separated). Is any of the above scenarios more useful for a rigorous model object model? Using Hibernate has been a huge PITA for things like Lazy Initialization Exceptions.
I am editing this question in the hope of continuing the discussion (not sure if I am doing it right):
The problem with model objects is that they are not flexible at all. The comment below says that the application should be designed so that model objects can be used at all levels. What for? If the user wants a piece of ridiculous functionality, I must tell them: "Well, that will not work with model objects"?
Normal and simple, there is only one time when the model objects will not work. You may have:
public class Teacher { List<Student> students; [tons of other Teacher-related fields] } public class Student { double gpa; [tons of other Student-related fields] }
but maybe you don’t need all this information. You just need the name of the teacher, the number of students they are teaching this year, and the average GPA for all students. What would you do in this case? Get complete information about teachers and student relationships, and then your code gets an account in the student list and then calculates the total average of all gp inside? This looks like waaaay more effort than just creating a DTO with "String lastName", "int numStudents" and "double combinationGpa;
It may seem that my mind was composed on them, but I still have to work in an application where model objects can be fully used in each case. Regular real-world applications with non-standard user queries simply do not work.
model dto
sma Apr 21 '10 at 3:14 2010-04-21 03:14
source share