We are working on an MVVM application. Jersey (backend) exposing a REST interface and AngularJS (frontend) being the only consumer of this interface. The application uses a complex database model (data model), which is different from the model presented to the user in the browser (view model).
Currently we have came across the following dilemma:
- to introduce Data Transfer Object (DTO) classes that will contain all the required attributes to invoke complex business logic operations on our model
- expose the existing complex data model without introducing DTO classes
Approach 1 will allow to simply the clients of the REST interface, as they will not have to understand the data model and will only have to provide the values of all the required attributes in the DTO classes. However, this approach requires an introduction of a mapping layer in the backend, from/to the data model to/from the view model.
Approach 2 results in less code in the backend, but will probably make the code in the frontend more complex as it will have to do the mapping of user input to the existing data model.
From the REST design perspective approach 2 seems to be better, but is exposing the data model in REST always the best solution?