Designing classes to populate model in spring MVC

Hi,
I want to populate data into the model from inside a controller.
Suppose there is a controller method  fetchResponse(Task task) That takes some Task object...
and returns a json TaskResponse object.

now here could be one implementation :

fetchResponse(Task task  @(RequestParam))
{

TaskResponse taskResponse = new TaskResponse(task);
return serialize taskResponse.
}

TaskResponse {

attributes.
.
.
.
TaskResponse(Task task)
{
populate attributes
}

This is one way in which all the attributes of the TaskResponse object will be filled inside the TaskResponse object which will be serialized and sent .

Please suggest any pros and cons of this aproach... Also any alternatives.

Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
Why do you need serialisation? Do you send the response to another server? Why not simply set the Task to Model itself?

model.addAttribute("taskResponse", taskResponse);

Open in new window

0
Rohit BajajAuthor Commented:
The spring method is @ResponseBody
And so i can just send the TaskResponse object. probably spring serializes it automatically. Its about building the TaskResponse Object and then returning it.
The main question is about the design of code.  one way is  inside the controller only i do :
TaskResponse = new TaskResponse();
taskREsponse.set(0
taskResponse.set()
some looping to populate value
taskResponse.set(value)

This pollutes the controller.
On the other hand the taskREsponse depends on task Object
So i can just do taskResponse = new TaskResponse(task);
And inside the constructor of the TaskResponse() i build up the TaskResponse object so the login for taskResponse stays inside TaskResponse.
This way the controller is not polluted with the code.
Wanted expert opions on this way the pros and cons some are cleaer..
And other alternative ways..
Thanks
0
girionisCommented:
Ah.. ok, i see what you're doing. I thought you were using actual serialisation.

I would go with the second option,
new TaskResponse(task);

Open in new window

and populate the TaskResponse with the Task object in the constructor. You keep the controller cleaner and this is the usual way to populate objects if you know all the values at once.

An alternative would be to use the builder pattern if you have different parameters every time and you only need to populate some of them.

You could also consider static factory methods instead of passing the Task inside the constructor of TaskResponse.

By the way, these techniques (builder pattern and static factory methods) are described in detail in the book "Effective Java" (by Joshua Bloch). If you're concerned about writing better code and having a better design in you code I would definitely recommend it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
girionisCommented:
This is the correct answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.