PDA

View Full Version : command object vs modelobject


lasher169
Sep 13th, 2007, 08:50 PM
Hi,

I understand command objects and form objects are really the same thing. What I would like to know is when using spring mvc is there a necessity to maintain command object/form objects or can I just use the model objects to collect data from the form as if it was a command/form object.

Under struts you would collect the data into a form bean(Form object) and then copy it into your model objects before applying data manipulation. Please let me know which is the best approach.

Thanks

Chris

Jörg Heinicke
Sep 14th, 2007, 01:14 AM
I do not really know what you are after ...

Spring manages the command or form-backing object for you. If you don't break it (usually by instantiating ModelAndView instance yourself) it gets added correctly and automatically to the model. On a submit request Spring does the binding of request parameter values to the command object for you. So actually you need nothing to do yourself, just provide a command object - and avoid breaking the default method flow.

Joerg

lasher169
Sep 14th, 2007, 05:10 AM
what is the difference between a domain object, form object?

what I want to know do I need a form object can I use a domain object to be a form object as well.

Thanks

Marten Deinum
Sep 14th, 2007, 05:36 AM
You can use your DomainObject as a command object. However make sure that you know what you are doing, especially in managed environments like Hibernate and/or JPA backed objects.

TrevorIngs
Sep 14th, 2007, 05:58 AM
what is the difference between a domain object, form object?

what I want to know do I need a form object can I use a domain object to be a form object as well.

Thanks

I dont think there is an exact answer to your question, it really depends on what your doing IMHO. For example, if you need to display a single property from serveral "domain" objects then would you expose each to the UI layer?

Generally speaking, what has "worked best" for myself has been to create a composite command object to the UI. The command object may have domain objects, or simple property values (coming from a range of sources), example


public class EditMovieCommand {

//domain object
private Movie movie;
//some view related data
private boolean editView;
//id of domain object
private Long storeId;

//getters and setters
...
}


I treat the command as a container for the "stuff" (love that word), that I would only load once (from a domain perspective) - formBackingObject or createFormObject.

I feel this has quite a few advantages and makes adding additional "once off" values at later stages to your UI easier without introducing DTOs.

Jörg Heinicke
Sep 14th, 2007, 09:37 AM
what I want to know do I need a form object can I use a domain object to be a form object as well.

You get into architectural discussions here, there have been a few threads in the architecture forum about using DTOs or domain objects in the web layer. Unfortunately, you can't search (http://forum.springframework.org/showthread.php?t=22782) for "dto" with this forum.

Joerg

lasher169
Sep 14th, 2007, 04:52 PM
thanks for the reply...

I am striving to see what is the right and wrong way of doing things. Been coding for so long and all these new things coming out I tend to get lost with all the conventions.

Chris

lumpynose
Sep 16th, 2007, 01:48 PM
thanks for the reply...

I am striving to see what is the right and wrong way of doing things. Been coding for so long and all these new things coming out I tend to get lost with all the conventions.

Chris

Hee hee! Welcome to the club.

In addition to your question I also have problems figuring out where to put my classes; i.e., in what package. For example, I had to make a simple data transfer object for adding the user's password to the db, containing just the user's id and their password. Where do I put that dto, in the dao packages or the service layer packages? I ended up making a package in the dao tree named dto but I don't know if that's right or not.

lumpynose
Sep 16th, 2007, 01:51 PM
Also, in one of the first chapters in the book Expert Spring MVC and Web Flow by Seth Ladd he gives a nice explanation of how to slice and dice and think about the different layers.