0

In my application written in Hibernate and Spring MVC I've got simple classes representing Customer, his main office Address and list of Locations of rest of his offices. There is a form that allows to add and edit data of Customer, then Spring MVC maps it to DTO, validates, sends down to Service which converts it to (slightly different) Entity and saves.

I thought about using value objects for representing and implicit validation for some objects - i.e. PhoneNumber, Address, but I don't really know how to use them in these frameworks - even if I think I understand the Value Object concept and immutability.

If I will use them in DTOs used in presentation layer then I can't rely on Spring mechanism of mapping fields from form to object - because there are no setters. And I must do the mapping manually in Controller or Service.

Should I use Value Objects in Entity (embed them)? Or maybe this is too simple case to use Value Objects at all?

mdziob
  • 1,116
  • 13
  • 26

2 Answers2

0

Your question is not primarily about value objects, but your main concern (if I understand correctly) is how to send data from a web form, validate the data and save the data in entities.

Create a form object such as MyForm with all fields that the user can enter in your screen. The form object serves to transport your data to the server, for example:

class MyForm {

    @NotBlank
    private String name;

    @NotBlank
    private String phoneNumber;

    @NotBlank
    private String address;

    // getters and setters
}

On the server side controller validate, convert and store the data:

@Controller
class GuestbookController {

    @RequestMapping(value = "/guestbook", method = RequestMethod.GET)
    String myget(Model model, MyForm form) {
        // put data on model
    }

    @RequestMapping(value = "/mysubmit", method = RequestMethod.POST)
    String mysubmit(@Valid MyForm form, Errors errors, Model model) {

        if (errors.hasErrors()) {
            return myget(model, form);
        }

        myrepository.save(new MyEntity(form.getName(), form.getPhoneNumber(), form.getAddress()));
        return "redirect:/myview";
    }

}

You probably need more details about the address than what fits in a single String. Then, yes, using a value object containing all the address details is fine.

Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
-1

here is my humble response.

  • DTOs are simple data containers with no logic,
  • and value objects are the opposite and encapsulate the logic.

Just a funny thing, DTOs were called value objects in the past in Java.

One way to see it (as I read it) is to let value objects encapsulate all the logic for entities, entities that handle only identity.

DTOs allow decoupling the domain model from the UI (or any other ports/adapters).

Should value objects be used outside the domain layer ? I don't know but personally I don't do it. In my setups, they are used by the application layer and inside the domain, but outside the application layer there is only DTOs.

I have DTOs that Spring can handle and data mappers to do the translation (dto to model at the UI/app layers border). The DTOs generally can have setters (event public property according to some people).

Now, if you want to use immutable object mapped by the framework, the solution depends on the technology you are using. When I do webservices with JSON (and Jackson), I map immutable commands object directly with @JsonCreator annotation on constructor but I couple my pojos to jackson. To avoid this, you can also add your customer serializers to the framework to let it convert automatically the data structure around controller method parameters.

Otherwise, you always can do mapping with data mapper or factory methods. (I believe you wrote a post about that...)

chikincrow
  • 393
  • 3
  • 11