I've to implement some Backend Webservices, which provide a given, final JSON structure, which is allready in use on the FrontEnd side. This structure doesn't match the database structure, so I have to convert the database entities to the needed serialization format.
It's a service on demand, that means the backend services has to determine and calculate the situation based on a user given time. So i cannot return the selections from the repository directly to the user, instead I need some additional layers for the business code (determining and calculation the situation on time X).
Here are my database tables
entry
-------------
id int (PK)
result_id (FK)
...
result
-------------
id int (PK)
t1 double precision
t2 double precision
t3 double precision
t4 double precision
total double precision
The response has to look like that:
entry: {
id: x,
currentResult: {
id: x,
t1: xx.xxx,
t2: xx.xxx,
t3: xx.xxx,
t4: xx.xxx,
total: xx.xxx
},
lastResult: {
id: x,
t1: xx.xxx,
t2: xx.xxx,
t3: xx.xxx,
t4: xx.xxx,
total: xx.xxx
}
}
So here my questions:
At the moment I select all needed data into DTO's, not into the entity records. The DTO's have already the structure of the output format (e.g. EntryDto has already current and last lap, which isn't possible in the Entry-Entity). Now all calculations will be made directly in the DTO (which is actually wrong, because it should be responsible only for transfering the data). This approach works good, and for me the code has a clean structure, because the business code is completely in the DTO (which however is wrong).
If i wanna be some for conform to good practices, I have to move the business code into another layer. But what layer should be responsible for that?
- Entity: It's not the right spot, because some related properties which are needed for the calculations are only present in the DTO.
- DTO: All properties for the calculations are given, but the DTO shouldn't be so complex with the amount of methods / business logic.
- ?
Would be the following a better solution?
- Entity entry is only responsible for database internals (joins,...)
- Repository selects data directly into new layer EntryBo (EntryBusinessObject) by constructor expression. All calculations will be made in those BO's.
- After all calculations were made transform the BO's into DTO's and finalize the response
Note: The above project is only a light example. Please no notes on that, only on the given problems.