My application loads lots of data from a database into a complex data structure. The in-memory data structure ressembles the structure of the database, which means that if the database contains the following tables:
Then I have classes A, B and C, and:
This implies that if I load the database, that I have to load it in the correct order. If I first load C, then it will complain that it cannot set the value C::m_b because the instance where it should point to was not loaded.
Problem is when there is also a column in A that is a foreign key to one of the other tables, let's say C.
I could solve the problem by loading all foreign keys as strings, and then perform a lookup after all the data has been loaded, but since I sometimes have to load millions of records, I can't afford to spend memory on these (albeit temporary) strings.
Having read about good design (e.g. the book "Large Scale C++ Software Design") it seems to me that it's a bad idea to have circular references at all. E.g. if file X.H includes Y.H, but Y.H also includes X.H you probably have a bad design; if class X depends on class Y and vice versa you probably have a bad design, which should be solved by extracting this dependency and introducing a third class Z, which depends on X and Y (X and Y won't depend on eachother anymore).
Is it a good idea to also extend this design-rule to database design? In other words: preventing circular references in foreign keys.
The only time you should need a circular reference is when you are creating a hierarchical structure, such as an organizational tree.
Table Employees
EmployeeID <----------|
SupervisorEmployeeID ---|