Hello.
I am wondering for some time, why majority is proposing DTO pattern for communication with presentation client in 3-tire architecture. Such solution is also described in hibernate wiki page as dominant.
Among others I read following conversations:
http://forum.hibernate.org/viewtopic.ph ... al&start=0
http://forum.hibernate.org/viewtopic.ph ... teexternal
http://forum.hibernate.org/viewtopic.ph ... teexternal
http://www.hibernate.org/124.html
Problem that bugs me the most is complexity of DTO objects. If ER model is complex, there are alot of different associations, that have associations, that have associations... Building such DTO framework with ability of transformation DTO <-> ORM takes some time. Same goes for sending graph data from client to server when you try to save DTO graph that comes from client to ORM graph. Why is it worth the time once it is implemented and used on several projects (doesn't same apply for Hibernate Bean DTOs)?
Off course there are some advantages with DTOs:
- composite data
- no lazy initialization error
- some method can be hidden from client
- ...
On the other hand you can override writeExternal and skip unitialized proxies with Hibernate.isInitalized(object) and just transfer Hibernate objects over wire. Maybe you can even build interface over ORM objects and return only wanted functionality to client. e.g.:
Code:
public class OrmObject1 implements ORMObj1Intf {
public Set<OrmObject2> getOrmObject2s()
{
...
}
public void setOrmObject2s(Set<OrmObject2>)
{
}
public int getId() {
...
}
public setId(int id) {
...
}
public void serverBusinessMethod() {
...
}
}
// hide business method from client
public interface OrmObj1Intf ()
{
public void getId();
public void setId(int id);
public Set<OrmObject2Intf> getOrmObject2s();
public void setOrmObject2s(Set<OrmObject2Intf> objs);
}
public interface OrmObj2Intf ()
{
...
}
public class OrmObj2() implements OrmObj2Intf
{
...
}
Does original Hibernate object stay in persistent context?
What are disadvantages of this approach for tranferring data? We give client different interface and also no DTO layer with Assemblers must be build. Maybe this approach could be used in Command pattern?
Thanks for your replies.