LINQ to SQL Custom Code Review
Define Person, Role and Salary Classes
Code Download
- Download Description:linq to sql custom download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:41 KB
Code Review
- LINQ to SQL Custom Code Review: Define Person, Role and Salary Classes
- LINQ to SQL Custom Code Review: Mapping the Class and Database Objects Using XML Mapping File
- LINQ to SQL Custom Code Review: Define Custom DataContext Class
- LINQ to SQL Custom Code Review: Manipulate the Database using the DataContext Object
Code Walkthrough
Define the Person class which will map to the Person database table.
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 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 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 EntitySet and EntityRef objects are initialized using the "default" keyword, which will return null for reference types and zero for numeric value. So these types are set to null.
When the Role property is updated, the EntityRef type storing the associated role is set to null and the Person object is removed from the corresponding EntitySet collection in the Role object. Then the EntityRef type storing the associated role is updated with the value of the role and the Person object is appended to the corresponding EntitySet collection in the associated Role object.
Define the Role class which will map to the Role database table.
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 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.
The Attach_Person method sets the Role object of the associated Person object. The Detach_Person method clears the Role object of the associated Person object.
Define the Salary class which will map to the Person database table.
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 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.
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!