Alter the domain object to have virtual properties and a no-argument constructor. When NHibernate loads objects, it does so “lazily” by default. This means that if a request is made to load an object, NHibernate actually returns a proxy to the object. Only when the object is used in some way is an actual trip to the database made. This behavior can be modified on a case by case basis and will improve performance in some cases. To facilitate StaffMember objects being lazily loaded by NHibernate, its class properties and methods need to be defined as virtual. So under Northwind.Core, modify StaffMember.cs as follows:
[DomainSignature]
public virtual string EmployeeNumber { get; protected set;}
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
Since you hadn’t done this before, NHibernate was complaining that the properties need to be defined as virtual to work with the default, lazy behavior. (To learn more about the proxy design pattern in general, see http://www.dofactory.com/Patterns/PatternProxy.aspx.)
In addition to needing virtual properties to support the lazy loading of an object, NHibernate also requires that the domain object have a no-argument constructor in order to create and hydrate the object with data from the database. As we do not want anyone creating a StaffMember object without supplying an employee number, we can simply add a protected no-argument constructor, which NHibernate can access, as shown in the first line below:
protected StaffMember() { }
public StaffMember(string employeeNumber) {
Check.Require(!string.IsNullOrEmpty(employeeNumber) &&
employeeNumber.Trim() != string.Empty,
"employeeNumber must be provided");
EmployeeNumber = employeeNumber;
}
In this way, our domain logic is enforced while allowing NHibernate to load the object without difficulty.