Knight Data Access Layer
Repository Pattern
Basic Tasks C# > Patterns > Repository Pattern

The BaseRepository is built into Knight Data Access Layer. 

 

Here is an example of the usage for the repository pattern:

 

    public class PersonRepository : BaseRepository<Person>, IPersonRepository

    {

        public void Update(Person updatedPerson)

        {

            Save(updatedPerson);

        }

        public Person Get(int id)

        {

            return LoadOne(id);

        }

        public List<Person> GetAll()

        {

            return LoadAll();

        }

        public void Post(Person person)

        {

            Save(person);

        }

        public void Delete(int id)

        {

            base.Delete(id);

        }

    }

 

Below is the source in it's entirety for the BaseRepository which can be modified to suit your needs:

 

/// <summary>

/// Implements Repository Pattern

/// </summary>

/// <typeparam name="T"></typeparam>

public class BaseRepository<T> where T : class, new()

{

#region Properties

/// <summary>

/// Expose data helper that has the full range of capabilities

/// </summary>

public IDataHelper Helper

{

get { return DataHelper.SessionFactory(); }

}

#endregion

 

#region Methods

/// <summary>

/// Insert or Update a record depending upon if the primary keys is the default value

/// </summary>

/// <returns></returns>

public void Save(T entity)

{

Helper.Save(entity);

}

/// <summary>

/// Save an object with its parent and children

/// </summary>

public void SaveWithChildren(T entity)

{

Helper.SaveWithChildren(entity);

}

/// <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(T entity)

{

Helper.Delete(entity);

}

/// <summary>

/// Delete the corresponding record in the database and all its children

/// </summary>

public void DeleteWithChildren(T entity)

{

Helper.DeleteWithChildren(entity);

}

/// <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

}