I’m having an issue with the way I’m going to do this and I’ve been thinking about it for the last couple of days.
I have this entity object, say a Person object, and each person has a many-to-one mapping of say, an Employer object. Now I know a person can technically have more than one employer, making this a many-to-many relationship, but I’ll say for my purposes that each Person can have only one Employer but each Employer can have many Person objects associated with it. This is most likely the case in the real world anyway.
I’ve created some entity classes for objects such as these and I’ve come across a problem. How will I persist the Employer relationship for any given Person object. For example, Person A has a name, birthdate, and Employer ID. However, in my Person entity (my POCO), I’d like to have each Person have an Employer object rather than just an Employer ID. The difference would be like this:
personA.EmployerID vs personA.Employer.ID, personA.Employer.Name, personA.Employer.Address, etc.
I can do this, but I want lazy loading. In other words, I don’t want the Employer object to be fetched from the database until it is used by invoking personA.Employer.<value> for the first time for any given instance of the Person class. The reason for this is the performance cost that eagerly loading all the dependent objects of any given entity might incur. It wouldn’t be so bad if the only dependency here was the Employer object, or even other laterally dependent objects within the Person class, such as personA.Pets, or personA.Girlfriend. However, what happens when I have a ladder of dependencies such that Person has an Employer, and Employer has an associated CEO object and a BoardOfDirectors object and an Address object. BoardOfDirectors then has a person object for each position and each position has a PhoneNumber object and on and on. So now when I want to load a person’s name, I will also have to reach into an Employer table, an Address table, a BoardOfDirectors table, and a PhoneNumbers table, at minimum! This is why I need lazy loading if I’m going to be doing things this way. That way when I want personA’s name, the only table I will need is the People table and I’d only need other tables when I call the related properties from my personA instance.
The other problem I have is saving a person’s Employer object when it’s changed. Really, in the People table all I’m going to be having to associate the Person to the Employer is an Employer ID. However, my POCO will have an Employer type, not an Int type EmployerID asociated with it. But when I want to save a new Employer to personA, all I will need to do is change the EmployerID value in the People table to correspond to a matching Employer row in the the Employers table. However, I don’t want to have to pull from the Employers table just to change the peopleA.Employer object only to need just the ID for the People table’s EmployerID column. This is a tough one because I know I can just add an EmployerID property as well, but then it becomes kind of unclear which to use. I could make the EmployerID a mutatable only property maybe? Then the Employer object could be accessible only; not mutatable. I don’t know. I’ll think about it.
