Question

I have a very simple question.

In Java code, I used to use Data Transfer Objects for Requests / Responses.

For example, in my Spring webapp I was creating some request dto, like

public class SaveOfficeRequest {
    private String officeName;
    private String officePhone;
    private String officeAddress;

    /* getters / setters */
}

After that i had controller with "mapped" method like

@ResponseBody
public SaveOfficeResponse saveOffice(@RequestBody SaveOfficeRequest) { ... }

.

Every request is json request. When some controller method was called i converted request dto to domain dto entities and do some business logic.

So!

Should I save the practice in my new scala project based on Play Framework?

Était-ce utile?

La solution

Case classes can be used to represent the request and response objects. This helps make the API explicit, documented and type-safe, and isolate concerns, by avoiding to use domain objects directly in external interface.

For example, for a JSON endpoint, the controller action could use a pattern like this:

request.body.asJson.map { body =>
  body.asOpt[CustomerInsertRequest] match {
    case Some(req) => {
      try {
        val toInsert = req.toCustomer()  // Convert request DTO to domain object
        val inserted = CustomersService.insert(toInsert)
        val dto = CustomerDTO.fromCustomer(inserted))  // Convert domain object to response DTO
        val response = ... // Convert DTO to a JSON response
        Ok(response)
      } catch {
        // Handle exception and return failure response
      }
    }
    case None => BadRequest("A CustomerInsertRequest entity was expected in the body.")
  }
}.getOrElse {
  UnsupportedMediaType("Expecting application/json request body.")
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top