Consider the following type of Java / Spring web application, with an SQL database:
- there are multiple data entity types (about 100) with relations between them
- the entities are viewed, edited or exposed to APIs, and frequently this happens with several entity types (joined)
The current approach uses three layers:
- a data layer, that queries the tables, and uses entities that are 1:1 match to the database
- a service layer to perform the business logic, and call the data layer as needed
- a controller layer - exposing operations to the client side code and to the API
My questions related to handling the models are:
- Should every layer have its own models / entity classes? If yes, how is it best to handle copying / merging the models across the layers?
- Sometimes, at the service layer, an entity might require to have certain fields filled in one case, but not in other cases. Should there be two model classes for these two situations? (To make sure you can count on what fields are provided by the service)
- Given the large number of entities, is it worth to be consistent in addressing the issues above in the same manner, regardless of any extra complexities involved?