NHibernate Entities
See the Sample hbm mapping for these entities. Mostly, this is just a POCO (plain old CLR object).
- You need a default constructor (can be "protected")
- For lazy loading (default), all properties should be virtual.
- Normal practice until v3.2 (April 2011) was to have collection properties with private setters. From v3.2, it's not allowed- make them protected or protected internal.
- You can get away with it for a while, but eventually you'll run into collection bugs because you need to override GetHashCode and Equals. A base class solves this easily (it's not pure POCO, but it's not a NHibernate dependency). Here we use EntityBase
Category
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Northwind
{
/// <summary>
/// Class representing Categories table
/// </summary>
[Serializable]
public class Category : EntityBase<int, Category>
{
#region Fields
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IList<Product> _productCollection = new List<Product>();
#endregion
#region Properties
/// <summary>
/// CategoryName (Required)
/// </summary>
public virtual string CategoryName { get; set; }
/// <summary>
/// Description
/// </summary>
public virtual string Description { get; set; }
/// <summary>
/// Picture
/// </summary>
public virtual System.Byte[] Picture { get; set; }
/// <summary>
/// Foreign Key Collection
/// </summary>
public virtual IList<Product> ProductCollection
{
get
{
return _productCollection;
}
}
#endregion
#region overrides
public override string ToString()
{
return "Category = " + Id;
}
#endregion
} //class
} //namespace
Product
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Northwind
{
/// <summary>
/// Class representing Products table
/// </summary>
[Serializable]
public class Product : EntityBase<int, Product>
{
#region Properties
/// <summary>
/// ProductName (Required)
/// </summary>
public virtual string ProductName { get; set; }
///// <summary>
///// Foreign Key Entity for SupplierId (Optional)
///// </summary>
//public virtual Supplier Supplier { get; set; }
/// <summary>
/// Foreign Key Entity for CategoryId (Optional)
/// </summary>
public virtual Category Category { get; set; }
/// <summary>
/// QuantityPerUnit
/// </summary>
public virtual string QuantityPerUnit { get; set; }
/// <summary>
/// UnitPrice
/// </summary>
public virtual decimal? UnitPrice { get; set; }
/// <summary>
/// UnitsInStock
/// </summary>
public virtual short? UnitsInStock { get; set; }
/// <summary>
/// UnitsOnOrder
/// </summary>
public virtual short? UnitsOnOrder { get; set; }
/// <summary>
/// ReorderLevel
/// </summary>
public virtual short? ReorderLevel { get; set; }
/// <summary>
/// Discontinued (Required)
/// </summary>
public virtual bool Discontinued { get; set; }
#endregion
#region overrides
public override string ToString()
{
return "Product = " + Id;
}
#endregion
} //class
} //namespace