I have an application that gets a car entity from a third party database. I call the entity ThirdPartyCar
. My application needs to create a Car
entity by using data from a ThirdPartyCar
. However, the Car
entity must also derive some of its data from my application's database. For example, a status of a ThirdPartyCar
might be _BOUGHT
and through a database lookup my application must transform to Sold
.
I currently have a Car
constructor that has a ThirdPartyCar
argument. But the Car
constructor cannot populate the lookup data since it is an entity and entities should not have a reference to a repositories. So, I also have a service to populate the remaining data:
public class ThirdPartyCar {
@Id
private Long id;
private String vin;
private String status;
// more props + default constructor
}
public class Car {
@Id
private Long id;
private String vin;
private CarStatus status;
// more props (some different than ThirdPartyCar) + default constructor
public Car(ThirdPartyCar thirdPartyCar) {
this.vin = thirdPartyCar.getVin();
// more props set based on thirdPartyCar
// but props leveraging database not set here
}
public class CarStatus {
@Id
private Long id;
private String status;
}
public class CarBuilderService {
private final CarStatusMappingRepository repo;
public Car buildFrom(ThirdPartyCar thirdPartyCar) {
Car car = new Car(thirdPartyCar);
CarStatus status = repo.findByThirdPartyCarStatus(thirdPartyCar.getStatus());
car.setStatus(status);
// set other props (including nested props) that depend on repos
}
}
The logical place to create a Car
based on a ThirdPartyCar
seems to be the constructor. But I have a disjointed approach b/c of the need of a repo. What pattern can I apply such that all data is created in the constructor but still not have the entity be aware of repositories?
Aucun commentaire:
Enregistrer un commentaire