NHibernate Integrated Queries 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_id; private Int32 m_idRole; private String m_lastName; private String m_firstName; private Role m_role; private IList<Salary> m_salary = new List<Salary>();
public Person() { }
public virtual Role Role { get { return this.m_role; } set { this.m_role = value; } } public virtual Int32 ID { get { return this.m_id; } set { this.m_id = value; } } public virtual Int32 IDRole { get { return this.m_idRole; } set { this.m_idRole = value; } } public virtual String LastName { get { return this.m_lastName; } set { this.m_lastName = value; } } public virtual String FirstName { get { return this.m_firstName; } set { this.m_firstName = value; } } public virtual IList<Salary> Salaries { get { return this.m_salary; } set { this.m_salary = value; } } }

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

The IList collection is used by NHibernate to store a collection of Salary objects. In the Person class, one Person object can be linked to many Salary objects. The Role object represents the role to which the person is associated to.

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

public class Role { private Int32 m_id; private String m_roleDescription; private ISet m_people;
public Role() { } public virtual Int32 ID { get { return this.m_id; } set { this.m_id = value; } } public virtual ISet People { get { return this.m_people; } set { this.m_people = value; } } public virtual String RoleDescription { get { return this.m_roleDescription; } set { this.m_roleDescription = value; } } }

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

The ISet class is a special collection used by NHibernate and implements the IEnumerable interface.

The ISet collection is used by NHibernate to store a collection of Person objects. In the Role class, one Role object can be associated with many Person objects.

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

public class Salary { private Int32 m_id; private Int32 m_idPerson; private Int32 m_year; private Double m_salaryYear; private Person m_person;
public virtual Int32 ID { get { return this.m_id; } set { this.m_id = value; } } public virtual Person Person { get { return this.m_person; } set { this.m_person = value; } } public virtual Int32 Year { get { return this.m_year; } set { this.m_year = value; } } public virtual 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 Person object represents the person to which the salary is linked to.