LINQ to SQL Integrated Code Review

Define Person, Role and Salary Classes

Code Download

Code Review

Code Walkthrough

Define the Person class which maps to the Person database table.

public class Person { private Int32 m_personID; private Int32 m_roleID; private String m_lastName; private String m_firstName;
private EntityRef<Role> m_role; private EntitySet<Salary> m_salaries;
public Person() { this.m_role = default(EntityRef<Role>); this.m_salaries = new EntitySet<Salary>( new Action<Salary>(this.Attach_Salary), new Action<Salary>(this.Detach_Salary) ); }
public EntitySet<Salary> Salaries { get { return this.m_salaries; } set { this.m_salaries.Assign(value); } } public Role Role { get { return this.m_role.Entity; } set { Role previousValue = this.m_role.Entity;
if ((previousValue != value) || (this.m_role.HasLoadedOrAssignedValue == false)) { if (previousValue != null) { this.m_role.Entity = null; previousValue.People.Remove(this); } this.m_role.Entity = value;
if (value != null) { value.People.Add(this); this.m_roleID = value.RoleID; } else { this.m_roleID = default(int); } } } } public Int32 PersonID { get { return this.m_personID; } set { this.m_personID = value; } } public Int32 RoleID { get { return this.m_roleID; } set { this.m_roleID = value; } } public String LastName { get { return this.m_lastName; } set { this.m_lastName = value; } } public String FirstName { get { return this.m_firstName; } set { this.m_firstName = value; } }
private void Attach_Salary(Salary entity) { entity.Person = this; } private void Detach_Salary(Salary entity) { entity.Person = null; } }

The object relational mapping of the Person object to the Person database table is handled in the XML Mapping file.

The EntitySet class is a special collection used by LINQ to SQL and implements the IEnumerable interface. The EntitySet class represents the many side of a one-to-many relationship. The object on the one side of a one-to-many relationship stores its associated many objects in the EntitySet type. So in the Person class, one Person object can be linked to many Salary objects, and the Salary objects are stored in the EntitySet type.

The EntitySet collection is initialized with handlers for add and remove operations. The Attach_Salary method is invoked when a Salary object is added to the EntitySet collection, and the Detach_Salary method is invoked when a Salary object is removed from the EntitySet collection.

The EntityRef class is a special collection used by LINQ to SQL and implements the IEnumerable interface. The EntityRef class represents the one side of a one-to-many relationship. The object on the many side of a one-to-many relationship stores its associated one object in the EntityRef type. So in the Person class, one Role object can be linked to many Person objects.

The Role property in the Person class defines specific logic to manage the collection of Person objects stored against a specific Role object. When the role property in the Person object is changed, the Person object is first removed from the EntitySet collection defined in associated Role object. Then the Person object is appended to the EntitySet collection in the newly associated Role object.

Define the Role class which maps to the Role database table.

public class Role { private Int32 m_roleID; private String m_roleDescription;
private EntitySet<Person> m_people;
public Role() { this.m_people = new EntitySet<Person>( new Action<Person>(this.Attach_Person), new Action<Person>(this.Detach_Person) ); }
public EntitySet<Person> People { get { return this.m_people; } set { this.m_people.Assign(value); } } public Int32 RoleID { get { return this.m_roleID; } set { this.m_roleID = value; } } public String RoleDescription { get { return this.m_roleDescription; } set { this.m_roleDescription = value; } }
private void Attach_Person(Person entity) { entity.Role = this; } private void Detach_Person(Person entity) { entity.Role = null; } }

The object relational mapping of the Role object to the Role database table is handled in the XML Mapping file.

The EntitySet class is a special collection used by LINQ to SQL and implements the IEnumerable interface. The EntitySet class represents the many side of a one-to-many relationship. The object on the one side of a one-to-many relationship stores its associated many objects in the EntitySet type. So in the Role class, one Role object can be linked to many Person objects, and the Person objects are stored in the EntitySet type.

The EntitySet collection is initialized with handlers for add and remove operations. The Attach_Person method is invoked when a Person object is added to the EntitySet collection, and the Detach_Person method is invoked when a Person object is removed from the EntitySet collection.

Define the Salary class which maps to the Salary database table.

public class Salary { private Int32 m_salaryID; private Int32 m_personID; private Int32 m_year; private Double m_salaryYear;
private EntityRef<Person> m_person;
public Salary() { this.m_person = default(EntityRef<Person>); }
public Int32 SalaryID { get { return this.m_salaryID; } set { this.m_salaryID = value; } } public Person Person { get { return this.m_person.Entity; } set { Person previousValue = this.m_person.Entity;
if ((previousValue != value) || (this.m_person.HasLoadedOrAssignedValue == false)) { if (previousValue != null) { this.m_person.Entity = null; previousValue.Salaries.Remove(this); } this.m_person.Entity = value;
if (value != null) { value.Salaries.Add(this); this.m_personID = value.PersonID; } else { this.m_personID = default(int); } } } } public Int32 PersonID { get { return this.m_personID; } set { this.m_personID = value; } } public Int32 Year { get { return this.m_year; } set { this.m_year = value; } } public Double SalaryYear { get { return this.m_salaryYear; } set { this.m_salaryYear = value; } } }

The object relational mapping of the Salary object to the Salary database table is handled in the XML Mapping file.

The EntityRef class is a special collection used by LINQ to SQL and implements the IEnumerable interface. The EntityRef class represents the one side of a one-to-many relationship. The object on the many side of a one-to-many relationship stores its associated one object in the EntityRef type. So in the Salary class, one Person object can be linked to many Salary objects.

The EntityRef object is initialized using the "default" keyword, which will return null for reference types and zero for numeric value. So the EntityRef type is set to null.

The Person property in the Salary class defines specific logic to manage the collection of Salary objects stored against a specific Person object. When the person property in the Salary object is changed, the Salary object is first removed from the EntitySet collection defined in associated Person object. Then the Salary object is appended to the EntitySet collection in the newly associated Person object.