1

what is the best approach to structure data model when using autowire? here is basic use case: i have domain data model on the backend which has case class User with fields id, username, active, auth_token which represents. This class maps to sql database table. On the frontend i only need fields id, username of User entity. I see only two approaches:

  1. map User class to some FrontUser class with fields i need
  2. divide database table on two separate which will be mapped to two different objects User and FrontUser
andresrcom
  • 23
  • 4

1 Answers1

1

This is mostly a matter of opinion -- there's likely no One True Answer to it. But from this description I'd recommend simply having User and FrontUser. Dividing the database table usually introduces additional latencies and complexity to the code, whereas introducing a smaller case class for the API and front end is usually pretty small and easy -- possibly as little as a couple of lines of code. That seems likely to be easier both to write and maintain.

Justin du Coeur
  • 2,699
  • 1
  • 14
  • 19
  • What do you think of making two case classes, one if which is nested into another: `code` case class User(backend: UserBackend, username: String) case class UserBackend(id: Option[Long]) `code` does this make sense? – andresrcom Feb 09 '16 at 13:39
  • It's certainly reasonable; whether it's the way you want to go depends mainly on how you want to structure your code. It's largely subjective... – Justin du Coeur Feb 10 '16 at 13:48
  • +1 on keeping the database concerns separated from the API needs. And good of @andresrcom to figure out the need for not just blindly sending the full user class to the browser. – Per Wiklander Feb 10 '16 at 20:40