As I'm just learning all the fruits of Java and everything I wanted to find out one thing which was flying around in my mind for some time. The code bellow is quick example of two methods in two different classes. First one is obviously a controller for some page and the other one is part of service.
@RequestMapping("/something)
public void doSomething() {
...
SomeEntity example = new SomeEntity();
example.setAccount(account);
example.setSmthElse(else);
example.setDate(new Date());
example.setSomething(something);
someService.saveSomeEntity(example);
}
...
public void saveSomeEntity(SomeEntity object) {
someEntityDAO.save(object);
}
So my question here is should the creation of the new entity SomeEntity
and setting of it's properties be done in the presentation layer part as above or should it be done somehow in the saveSomeEntity
method by passing all the params to it?
The controller is not the presentation layer. Its the C in MVC. The persistence layer should only be concerned with persistence, not Model object creation. The code above is OK. Some would create the objects in the service, not in the controller, but the params are readily available in the controller so IMHO its acceptable.