1
votes

Let's imagine you create a Jhipster racing app where we have a Race class (id, raceName, date, List -Participant-, averageTime) which is made of Participant objects (which have an id and a racing time in seconds for each race). You would like to calculate the average time of the racing class as the times are released (as they cross the finish line) and everytime a race is consulted. For that, where would you put the method for calculating such an average?

I see 3 options:

  1. In the view of your angular class when you show your Race component you can bring data and calculate it each time it is shown.
  2. Somewhere in the Race Service Implementation.
  3. In the domain model with a @Transient when the Race object is instanciated

Please, if you see another options or a best practice let me know. Thanks

entity Race { raceName String, time Instant, averageTime Double }

entity Participant { racingTime Integer }

relationship OneToMany { Race{participant(id)} to Participant{race(id)} }

// SET PAGINATION OPTIONS: paginate all with pagination // paginate ChatMessage with infinite-scroll // paginate all with infinite-scroll

// SET SERVICE OPTIONS: service all with serviceImpl //service all with serviceClass

// DTO: dto all with mapstruct

// FILTERING: filter *

1

1 Answers

3
votes

My preference is to do this in the service layer as part of the conversion from domain model to DTO. You could implement a @Transient getter in the Race domain model which calculates the average and (I think) mapstruct could map this across to the DTO. Or you could just set the extra DTO field in your service method(s). There's also this where you can put the calculation in your mapper.