You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Hookom, Jacob" <Ja...@redline.mckhboc.com> on 2004/03/29 18:42:07 UTC

[OT] RE: best practice question for certain VO/DTO fields?

This has been discussed lots before; I usually opt for ViewObjects that
match what is needed for rendering.  It makes it really easy to make
modifications in the controller as to how the data is pulled while keeping
the view unchanged.

EmployeeView.getName()
EmployeeView.getDepartmentName()
EmployeeView.getDepartmentId()
.....

Then your action can initially do whatever you want for proper OO with a
domain model or whatever, but if you need to refactor later, you can have
that EmployeeView be generated straight from a ResultSet.

-----Original Message-----
From: Rick Reumann [mailto:struttin@reumann.net] 
Sent: Monday, March 29, 2004 10:23 AM
To: Struts Users Mailing List
Subject: best practice question for certain VO/DTO fields?

Curious about the different ways you guys handle this..

For simplicity, imagine you are needing to return a List of
Employee objects. Each Employee belongs to a department.

What I debate about is how to handle the creation of the
Employee Value Object in relation to "department." From a
business perspective I really only need to worry about an int or
Integer representing "departmentID", yet for display purposes
I'll often need to display "dpeartmentName." There seems to be
several ways to handle this...

1) Store a Department object (id, name) as a field inside of
Employee. This is nice from an OO perpsective, but can be a bit
more difficult to handle depending on the persistence mechanism
of choice. I tend to think this approach can create a bit of
unnecessary overhead.

2) Simply store another field in EmployeeVO as String
departmentName. I seem to end up doing this because it's simple,
but it just doesn't 'feel' right. The EmployeeVO only really
needs to know what departmentID it should have. The
departmentName is only important for display purposes.

3) Provide some way for your display to figure out the
departmentName based on a given departmentId (JSP tag, map
lookup, whatever). I don't really like this approach because if
the values can change (in this case Departments), it requires a
db lookup any time you want to get a departmentName (unless you
create some guaranteed way to have a cache updated). Plus this
is only an example and it's just one field, but potentially your
application can have a bunch of similiar things to deal with -
do you really want to have to create a bunch of tags or maps to
deal with this?

4) Return a List of Department objects and in each of those pull
out a List of Employees. Awkward to work with (some gymnastics
to get a complete employees display back ordered correctly).
Also department could contain tons of fields other than List or
Collection of Employees. Do we really want a whole bunch of
Department objects back just to get our employees? This also
a potential problem in option 1 above as well. Even if all the
fields of Department aren't populated, it still seems ugly
having this big object as part of an Employee. 

Thanks for ideas on how you guys handle this.

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: [OT] RE: best practice question for certain VO/DTO fields?

Posted by Robert Taylor <rt...@mulework.com>.
Yep.

For a quick code example:

Iterator it = // get a list of employees that match some criteria

while (it.hasNext()) {

 /*
  * Each EmployeeVO only contains its primary data
  */
 EmployeeVO vo = (EmployeeVO) it.next();

 /*
  * EmployeeVO lazily loads Department (only primary data)
  */
 DepartmentVO deptVO = vo.getDepartment();

 /*
  * Department lazily loads fooBarList
  */
 Collection foobars = deptVO.getFooBarList();
 
}


robert

> -----Original Message-----
> From: Rick Reumann [mailto:struttin@reumann.net]
> Sent: Monday, March 29, 2004 1:33 PM
> To: Struts Users Mailing List
> Subject: Re: [OT] RE: best practice question for certain VO/DTO fields?
> 
> 
> On Mon, Mar 29, 2004 at 01:18:12PM -0500, Robert Taylor wrote:
> 
> > couldn't you use a lazy loading scheme such that the 
> > Department reference in each Customer is not "loaded" until
> > accessed and even when the Department is "loaded" only the 
> > primary Department data is loaded (no object references).
> > So in essence, you only get what you need, when you need it.
>  
>     That sounds like a pretty good approach (although I admit I
>     haven't worked with lazy loading to that extent). So in
>     other words with the beans below:
> 
>     DepartmentVO
>     ------------
>     Integer id;
>     String name;
>     List fooBarList; //of some kind of other heavy objects
>     List fooBar2List; //of some kind of other heavy objects
> 
>     EmployeeVO
>     ----------
>     Integer id;
>     String name;
>     DepartmentVO dept;
> 
>     Now you are saying when displaying the list of Employees
>     only the id and name of the DepartmentVO might be loaded in
>     each Department, but if later I needed fooBarList a call to
>     that would then load up the List for me.
> 
>     I like that approach. 
> 
> -- 
> Rick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] RE: best practice question for certain VO/DTO fields?

Posted by Rick Reumann <st...@reumann.net>.
On Mon, Mar 29, 2004 at 01:18:12PM -0500, Robert Taylor wrote:

> couldn't you use a lazy loading scheme such that the 
> Department reference in each Customer is not "loaded" until
> accessed and even when the Department is "loaded" only the 
> primary Department data is loaded (no object references).
> So in essence, you only get what you need, when you need it.
 
    That sounds like a pretty good approach (although I admit I
    haven't worked with lazy loading to that extent). So in
    other words with the beans below:

    DepartmentVO
    ------------
    Integer id;
    String name;
    List fooBarList; //of some kind of other heavy objects
    List fooBar2List; //of some kind of other heavy objects

    EmployeeVO
    ----------
    Integer id;
    String name;
    DepartmentVO dept;

    Now you are saying when displaying the list of Employees
    only the id and name of the DepartmentVO might be loaded in
    each Department, but if later I needed fooBarList a call to
    that would then load up the List for me.

    I like that approach. 

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: [OT] RE: best practice question for certain VO/DTO fields?

Posted by Robert Taylor <rt...@mulework.com>.
Rick,

couldn't you use a lazy loading scheme such that the 
Department reference in each Customer is not "loaded" until
accessed and even when the Department is "loaded" only the 
primary Department data is loaded (no object references).
So in essence, you only get what you need, when you need it.

robert



> -----Original Message-----
> From: Rick Reumann [mailto:struttin@reumann.net]
> Sent: Monday, March 29, 2004 1:11 PM
> To: Struts Users Mailing List
> Subject: Re: [OT] RE: best practice question for certain VO/DTO fields?
> 
> 
> On Mon, Mar 29, 2004 at 10:42:07AM -0600, Hookom, Jacob wrote:
> 
> > This has been discussed lots before; I usually opt for ViewObjects that
> > match what is needed for rendering.  It makes it really easy to make
> > modifications in the controller as to how the data is pulled while keeping
> > the view unchanged.
> > 
> > EmployeeView.getName()
> > EmployeeView.getDepartmentName()
> > EmployeeView.getDepartmentId()
> > .....
> > 
> > Then your action can initially do whatever you want for proper OO with a
> > domain model or whatever, but if you need to refactor later, you can have
> > that EmployeeView be generated straight from a ResultSet.
>  
>    Well, it's not the view objects that I'm concerned with per
>    se. In other words your approach above still has to address
>    my main concern of what to return in the domain layer.
>    
>    From an OO standpoint nesting a Department object inside of
>    an Employee makes sense. Then, however, I look at it from a
>    practical standpoint - for example returning a large
>    Collection of Employees. If the Department object is a large
>    object you now have all the extra overhead of Department
>    objects nested inside each Employee. This isn't too big of a
>    deal if your business 'knows' that it only needs to populate
>    a few fields in Department (say id, and deptName), but if you
>    took a more generalized approach of populating everything in
>    Department, your Employee beans become extremely heavy.
>    
>    And if you don't take the generalized approach and your
>    queries do know exactly what the need to populate for the
>    view, then it's not a big deal to tack on a few extra fields
>    in the Employee Value Object. 
>    
>    I still don't see the need for an extra "EmployeeView"
>    object. I already would have an EmployeeVO and an
>    EmployeeActionForm. I don't see what the EmployeView object
>    would give you compared to using the VO. If it's formatting
>    concerns JSTL can handle all of that for me.
> 
> -- 
> Rick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] RE: best practice question for certain VO/DTO fields?

Posted by Rick Reumann <st...@reumann.net>.
On Mon, Mar 29, 2004 at 10:42:07AM -0600, Hookom, Jacob wrote:

> This has been discussed lots before; I usually opt for ViewObjects that
> match what is needed for rendering.  It makes it really easy to make
> modifications in the controller as to how the data is pulled while keeping
> the view unchanged.
> 
> EmployeeView.getName()
> EmployeeView.getDepartmentName()
> EmployeeView.getDepartmentId()
> .....
> 
> Then your action can initially do whatever you want for proper OO with a
> domain model or whatever, but if you need to refactor later, you can have
> that EmployeeView be generated straight from a ResultSet.
 
   Well, it's not the view objects that I'm concerned with per
   se. In other words your approach above still has to address
   my main concern of what to return in the domain layer.
   
   From an OO standpoint nesting a Department object inside of
   an Employee makes sense. Then, however, I look at it from a
   practical standpoint - for example returning a large
   Collection of Employees. If the Department object is a large
   object you now have all the extra overhead of Department
   objects nested inside each Employee. This isn't too big of a
   deal if your business 'knows' that it only needs to populate
   a few fields in Department (say id, and deptName), but if you
   took a more generalized approach of populating everything in
   Department, your Employee beans become extremely heavy.
   
   And if you don't take the generalized approach and your
   queries do know exactly what the need to populate for the
   view, then it's not a big deal to tack on a few extra fields
   in the Employee Value Object. 
   
   I still don't see the need for an extra "EmployeeView"
   object. I already would have an EmployeeVO and an
   EmployeeActionForm. I don't see what the EmployeView object
   would give you compared to using the VO. If it's formatting
   concerns JSTL can handle all of that for me.

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: [OT] RE: best practice question for certain VO/DTO fields?

Posted by Robert Taylor <rt...@mulework.com>.
+1.

Take a look at 
http://www.theserverside.com/books/wiley/EJBDesignPatterns/downloads/ejbdesignpatterns.pdf

more specifically these patterns:
Data Transfer Object Factory
Custom Data Transfer Object


robert




> -----Original Message-----
> From: Hookom, Jacob [mailto:Jacob.Hookom@redline.mckhboc.com]
> Sent: Monday, March 29, 2004 11:42 AM
> To: 'Struts Users Mailing List'
> Subject: [OT] RE: best practice question for certain VO/DTO fields?
> 
> 
> This has been discussed lots before; I usually opt for ViewObjects that
> match what is needed for rendering.  It makes it really easy to make
> modifications in the controller as to how the data is pulled while keeping
> the view unchanged.
> 
> EmployeeView.getName()
> EmployeeView.getDepartmentName()
> EmployeeView.getDepartmentId()
> .....
> 
> Then your action can initially do whatever you want for proper OO with a
> domain model or whatever, but if you need to refactor later, you can have
> that EmployeeView be generated straight from a ResultSet.
> 
> -----Original Message-----
> From: Rick Reumann [mailto:struttin@reumann.net] 
> Sent: Monday, March 29, 2004 10:23 AM
> To: Struts Users Mailing List
> Subject: best practice question for certain VO/DTO fields?
> 
> Curious about the different ways you guys handle this..
> 
> For simplicity, imagine you are needing to return a List of
> Employee objects. Each Employee belongs to a department.
> 
> What I debate about is how to handle the creation of the
> Employee Value Object in relation to "department." From a
> business perspective I really only need to worry about an int or
> Integer representing "departmentID", yet for display purposes
> I'll often need to display "dpeartmentName." There seems to be
> several ways to handle this...
> 
> 1) Store a Department object (id, name) as a field inside of
> Employee. This is nice from an OO perpsective, but can be a bit
> more difficult to handle depending on the persistence mechanism
> of choice. I tend to think this approach can create a bit of
> unnecessary overhead.
> 
> 2) Simply store another field in EmployeeVO as String
> departmentName. I seem to end up doing this because it's simple,
> but it just doesn't 'feel' right. The EmployeeVO only really
> needs to know what departmentID it should have. The
> departmentName is only important for display purposes.
> 
> 3) Provide some way for your display to figure out the
> departmentName based on a given departmentId (JSP tag, map
> lookup, whatever). I don't really like this approach because if
> the values can change (in this case Departments), it requires a
> db lookup any time you want to get a departmentName (unless you
> create some guaranteed way to have a cache updated). Plus this
> is only an example and it's just one field, but potentially your
> application can have a bunch of similiar things to deal with -
> do you really want to have to create a bunch of tags or maps to
> deal with this?
> 
> 4) Return a List of Department objects and in each of those pull
> out a List of Employees. Awkward to work with (some gymnastics
> to get a complete employees display back ordered correctly).
> Also department could contain tons of fields other than List or
> Collection of Employees. Do we really want a whole bunch of
> Department objects back just to get our employees? This also
> a potential problem in option 1 above as well. Even if all the
> fields of Department aren't populated, it still seems ugly
> having this big object as part of an Employee. 
> 
> Thanks for ideas on how you guys handle this.
> 
> -- 
> Rick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org