samedi 24 février 2018

How to avoid loading duplicate objects into main memory?

Suppose I am using SQL and I have two tables. One is Company, the other is Employee. Naturally, the employee table has a foreign key referencing the company he or she works for.

When I am using this data set in my code, I'd like to know what company each employee works for. The best solution I've thought of it to add an instance variable to my Employee class called Company (of type Company). This variable may be lazy-loaded, or populated manually.

The problem is that many employees work for the same company, and so each employee would end up storing a completely identical copy of the Company object, unnecessarily. This could be a big issue if something about the Company needs to be updated. Also, the Company object would naturally store a list of its employees, therefore I could also run into the problem of having an infinite circular reference.

What should I be doing differently? It seems object oriented design doesn't work very well with relational data.

This is more of a design/principles sort of question, I do not have any specific code, I am just looking for a step in the right direction!

Let me know if you have any questions.

Aucun commentaire:

Enregistrer un commentaire