Mapping the prerequisites

Classes, as we defined in the previous chapter, cannot be used with NHibernate as is. There are some prerequisites that every class must satisfy before NHibernate is able to persist that class to database. Persistent classes must satisfy at least following two requirements:

  • Every attribute on class that NHibernate should save in the database has to be declared as one of public virtual, protected virtual, and protected internal virtual. This is to make the lazy loading feature work. Lazy loading is an important feature. We will look into it in detail in Chapter 6, Let's Retrieve Some Data from the Database, where we learn about retrieving entities from database. For now, it is worth mentioning that lazy loading only works with virtual attributes of persistent classes. Having said that, let me also tell you that NHibernate is capable of working with private attributes as well, if the need be and we will look into that. But in most cases, you would not need to declare private attributes unless you are working with legacy databases. In order for lazy loading to work, every non-private method on the entity must also be declared as virtual.
  • Every entity that is persisted to its own table in database must have an identifier attribute. Identifiers are used to distinguish two instances of the same entity. Identifier maps to primary key of database table. Traditionally, databases have generated the identifier/primary key values automatically. But there could be some pitfalls to this approach. On the other hand, NHibernate is capable of generating identifiers using multitude of different strategies. We would examine these in some detail in the Identifier generation section of this chapter.

The first prerequisite is quite straightforward. We just need to add the virtual keyword to all the attributes and methods in our domain model. For the second prerequisite, we have multiple options really, but I want to keep this change simple for now. We would add an identifier attribute of type integer to all entities in our domain model. Since this is an attribute that appears on every domain class, we can refactor this into a common base class. Here is how that class looks:

public abstract class EntityBase
{
  public virtual int Id { get; set; }
}

Note that we have marked this class as abstract. We do not want this class to be used directly by any code. The purpose of this class is only to provide a common base class for domain entities. And here is what our Employee class changes to:

public class Employee : EntityBase
{
  public virtual string EmployeeNumber { get; set; }
  public virtual string Firstname { get; set; }
  public virtual string Lastname { get; set; }
  public virtual string EmailAddress { get; set; }
  public virtual DateTime DateOfBirth { get; set; }
  public virtual DateTime DateOfJoining { get; set; }
  public virtual Address ResidentialAddress { get; set; }
  public virtual bool IsAdmin { get; set; }
  public virtual string Password { get; set; }
  public virtual ICollection<Benefit> Benefits { get; set; }
}

For brevity, I have omitted other classes here but you get the crux, I hope. With the preceding code change, we are now ready to write mappings for our domain model. We will begin with a quick introduction to different mechanisms offered by NHibernate to write mappings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.249.210