2

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:

  1. A Person

    public class Person{
        Id: number;
        Name: string;
    
        Vehicles: Vehicle[];
    }
    
  2. A Vehicle

    public class Vehicles{
        Id: number;
        Model: string;
        CurrentOwnerId: number;
    
        CurrentOwner: Person;
        OwnerRecords: OwnerRecord[];
    }
    
  3. 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?

jarodsmk
  • 1,876
  • 2
  • 21
  • 40
  • I think that it's better change CurrentOwner:Person by CurrentOwnerId:number (where CurrentOwnerId is the id "key" of Person) – Eliseo Mar 27 '18 at 06:22
  • You don't need to use person everywhere instead you can use ownerRecord in both of the models. – Harshal Patil Mar 27 '18 at 06:22
  • @Eliseo - I'm not sure I quite understand your proposed solution? – jarodsmk Mar 27 '18 at 06:30
  • @HarshalPatil - I'll update my question, but i dont think I can do this because the current owner of a vehicle (given by Vehicle -> CurrentOwnerId) will not have an OwnerRecord yet. These would only be populated on change of ownership. – jarodsmk Mar 27 '18 at 06:32
  • @N15M0_jk, if your Persons is like [{id:1,name:"Bob"},{id:2,name:"Peggy"}], a Vehicle object can be like {id:0,model:"Ford",currentOwner:1,ownerRecord:[{id:2},{id:1}] – Eliseo Mar 27 '18 at 06:41
  • @Eliseo Hmm i see what you're saying, to clear up some confusion I'll modify my question a bit later – jarodsmk Mar 27 '18 at 06:54
  • 1
    I have the same problem. Many answers say you should have an personID in the vehicle model instead of a Person instance, but what about this example: that the vehicle has passengers array = Person[]. Then is not just an ID. – Bjarne Gerhardt-Pedersen Aug 24 '18 at 16:58

1 Answers1

0

I'm not sure you're taking your model the right way.

Given this (simplified) model

export class Person {
  id: string;
  full_name: string;
  current_vehicle_id: string;
}

export class Vehicle {
  id: strnig;
  brand: string;
  model: string;
  current_owner: string;
}

export class OwningRecord {
  id: string;
  person_id: string;
  vehicle_id: string;
}

If you want to view the ownership of a vehicle, you don't need to access any vehicle property. You have everything you want in the record, which is the users tied to the vehicle.

Simply display the list of records that match the vehicle.

If you want to display the list of vehicles of an user, again, you have the records for that. Simply show the list of vehicles that match the person.

If you follow this, you will have no circulat dependency in your case.

  • I see what you're trying to say, I'll update my question a bit later to define my models a bit better to avoid any confusion – jarodsmk Mar 27 '18 at 06:57