See: http://en.wikipedia.org/wiki/Active_record_pattern
//Class that inherits BaseActiveRecord
[
TableMapping("Persons")] public class Vendor : BaseActiveRecord<Vendor>{
PrimaryKey(PrimaryKeyType.Identity)] public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateCreated { get; set; }[
[
DefaultValue(1)] public int PersonTypeId { get; set; }}
Vendor vendor = new Vendor();
vendor.FirstName =
"John";vendor.LastName =
"Smith";vendor.DateCreated =
DateTime.Now;
//Save the vendor
vendor.Save();
/// <summary> /// Implements active record pattern for loading, saving and deleting records /// </summary> /// <typeparam name="T">Generic class type</typeparam> public class BaseActiveRecord<T> where T : class, new()
{
Properties /// <summary> /// Expose data helper that has the full range of capabilities /// </summary> public IDataHelper Helper#region
{
get { return DataHelper.SessionFactory(); }
}
#endregion
#region
Methods/// <summary> /// Insert or Update this record depending upon if the primary keys is the default value /// </summary> /// <returns></returns> public void Save()
{
this);Helper.Save(
}
/// <summary> /// Save an object with its parent and children /// </summary> public void SaveWithChildren(){
this);Helper.SaveWithChildren(
}
/// <summary> /// Save a list /// </summary> /// <param name="list">A list of items</param> public void Save(List<T> list){
Helper.Save(list);
}
/// <summary> /// Delete the corresponding record in the database /// </summary> public void Delete(){
this);Helper.Delete(
}
/// <summary> /// Delete the corresponding record in the database and all its children /// </summary> public void DeleteWithChildren(){
this);Helper.DeleteWithChildren(
}
/// <summary> /// Search for records based on the passed in search object and page in the database /// </summary> /// <param name="searchParameters">What to search for. Default values are not searched.</param> /// <param name="orderBy">Order by SQL Clause</param> /// <param name="page">The page number starting at 1</param> /// <param name="pageSize">The number of records per page</param> /// <returns></returns> public List<T> Search(T searchParameters, string orderBy, int page, int pageSize){
return Helper.Search(searchParameters, orderBy, page, pageSize);
}
/// <summary> /// Count the number of records for a search /// </summary> /// <param name="searchParameters">What to search for. Default values are not searched.</param> /// <returns></returns> public long SearchCount(T searchParameters){
return Helper.SearchCount(searchParameters);
}
/// <summary> /// Load all the records from a table into a class list /// </summary> /// <returns></returns> public List<T> LoadAll()
{
return Helper.LoadAll<T>();
}
/// <summary> /// Load all the records from a table into a class list /// </summary> /// <param name="orderBy">Order by SQL Clause</param> /// <returns></returns> public List<T> LoadAll(string orderBy){
return Helper.LoadAll<T>(orderBy);
}
/// <summary> /// Load the first record in a table /// </summary> /// <returns></returns> public T LoadOne(){
return Helper.LoadOne<T>();
}
/// <summary> /// Load a single object by the passed parameters /// </summary> /// <typeparam name="T">Generic class type</typeparam> /// <param name="parameters">The parameters to use for the where clause</param> /// <returns></returns> public T LoadOne(Dictionary<string, object> parameters){
return Helper.LoadOne<T>(parameters);
}
/// <summary> /// Load a record by primary key /// </summary> /// <returns></returns> public T LoadOne(object primaryKey){
return Helper.LoadByPrimaryKey<T>(primaryKey);
}
/// <summary> /// Load a record by the primary key with all children /// </summary> /// <param name="primaryKeyValue">The primary key value to search for</param> /// <returns></returns> public T LoadWithChildren(object primaryKeyValue){
return Helper.LoadWithChildren<T>(primaryKeyValue);
}
/// <summary> /// Load a page of records that are ordered /// </summary> /// <param name="orderBy">Order by SQL Clause</param> /// <param name="page">The page number starting at 1</param> /// <param name="pageSize">The number of records per page</param> /// <returns></returns> public List<T> LoadPage(string orderBy, int page, int pageSize){
return Helper.LoadPage<T>(orderBy, page, pageSize);
}
/// <summary> /// Load the primary key and the first text field. /// </summary> /// <returns></returns> public List<T> LoadLookup(){
return Helper.LoadLookup<T>();
}
/// <summary> /// Return the count of records in the table /// </summary> /// <returns></returns> public long Count(){
return Helper.Count<T>();
}
/// <summary> /// LINQ Provider /// </summary> /// <returns></returns> public IQueryable<T> CreateQuery(){
return Helper.CreateQuery<T>();
}
#endregion
}