Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Yes, the entity can be created at any layer.

Some people prefer DTOs (separate objects with the same structure) which are then translated to the entities.

Just avoid writing java code in the jsps. Instantiate the objects in the controller, or leave that to some binding mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top