I'm facing a challenge in Angular while trying to set up mock data. I have the following two classes:
export class Company {
public id: number;
public name: string;
public applications: Application[];
constructor(id: number, name: string, applications: Application[]) {
this.id = id;
this.name = name;
this.applications = applications;
}
}
export class Application {
public id: number;
public name: string;
public company: Company;
constructor(id: number, name: string, company: Company) {
this.id = id;
this.name = name;
this.company = company;
}
}
I've also created these services:
export class CompanyService {
companies = [
new Company(
1,
'Company 1',
[]
),
new Company(
2,
'Company 2',
[],
),
]
constructor(private http: HttpClient) { }
getAll(): Company[] {
return this.companies;
}
findById(id: number): Company | undefined {
return this.companies.find(company => company.id === id);
}
}
export class ApplicationService {
applications = [
new Application(
1,
'Application 1',
// COMPANY HERE SOMEHOW?
),
new Application(
2,
'Application 2',
// COMPANY HERE SOMEHOW?
),
]
constructor(private http: HttpClient) { }
getAll(): Application[] {
return this.applications;
}
findById(id: number): Application | undefined {
return this.applications.find(application => application.id === id);
}
}
Now, the issue arises when creating this mock data because I need to add a company to the constructor of an application. However, adding a company there requires that the object contain the application in its constructor as well. This creates a chicken/egg problem, and I am unsure how to resolve it.
Being new to Angular, I'm not sure if there's a workaround within the framework itself or if I should use IDs of the classes. Even with using IDs, I still face the same dilemma. Can anyone provide guidance on this matter?