Been working on a project that has circular dependencies amongst it's models. I gave this StackOverflow post and it's solutions a read, but as it suggests that circular dependencies are generally a case of mixed concerns, I don't think my scenario falls into that category.
So while they're just warnings, I thought I'd query whether this is the incorrect way to go about my following scenario:
Linked models
You have 3 models:
A Person
public class Person{ Id: number; Name: string; Vehicles: Vehicle[]; }
A Vehicle
public class Vehicles{ Id: number; Model: string; CurrentOwnerId: number; CurrentOwner: Person; OwnerRecords: OwnerRecord[]; }
A OwnerRecord
public class OwnerRecord{ Id: number; PersonId: number; VehicleId: number; PersonObj: Person; VehicleObj: Vehicle; }
A Person can own a Vehicle. The current owner of a Vehicle is given by an CurrentOwnerId field on the Vehicle. The OwnerRecord is a linking table which could represent the historical ownership data, and thus links Vehicles to People.
Use Case
On one screen, you might want to view the ownership history of a vehicle, thus the Vehicle model can have an OwnerRecord[]
property. As mentioned before, a Vehicle can also have a CurrentOwnerId
on it, and so, a CurrentOwner: Person
property.
A Person might want their Vehicle list shown, thus, they can have a Vehicle[]
property.
OwnerRecords obviously have a Person
and Vehicle
property.
The Current Owner of a vehicle will not have an OwnerRecord, as these will only be populated on changing of an owner, so this is purely for the historical owner records.
Interpretation
Surely this is not incorrect on the knowing that the Circular Dependency will never be fulfilled on the premise that I will never populate those objects as such after retrieving them from the backend?
How else would one go about this without getting those warnings at build time?