Creating generic repository

  • Implementation. The only thing you have to do is to write:
[Service]
public class EmployeeRepository : SharepointRepositoryBase<Employee
{
}
Fully customizable version: 
[Service(typeof(IRepository<Employee>), LifestyleType.PerWebRequest)]
public class EmployeeRepository : SharepointRepositoryBase<Employee>
{
}
Or with cache (see configuration):
[Service]
public class EmployeeRepository : SharepointRepositoryBaseWithCache<Employee
{
}
  • Then you get all the IRepository<T> methods:
public interface IRepository<T> 
where T : EntityBasenew()
{
    /// <summary>
    /// Gets all items
    /// </summary>
    /// <returns></returns>
    List<T> GetAllItems();

    /// <summary>
    /// gets item by unique id
    /// </summary>
    /// <param name="id"></param>
    /// <returns>Entity</returns>
    T GetItem(Guid id);

    /// <summary>
    /// Gets item by id
    /// </summary>
    /// <param name="id">Identifier</param>
    /// <returns>Entity</returns>
    T GetItem(int id);

    /// <summary>
    /// Saves item (inserts or updates)
    /// </summary>
    /// <param name="entity">Entity</param>
    void SaveItem(T entity);

    /// <summary>
    /// Deletes item
    /// </summary>
    /// <param name="id">Item identifier</param>
    void DeleteItem(int id);

    /// <summary>
    /// Deletes item
    /// </summary>
    /// <param name="id">Item idenditier</param>
    void DeleteItem(Guid id);

    /// <summary>
    /// Gets all items by id dictionary
    /// </summary>
    Dictionary<int, T> GetAllItemsByIdDictionary();

    /// <summary>
    /// Gets all items by guid dictionary
    /// </summary>
    Dictionary<Guid, T> GetAllItemsByUniqueIdDictionary();
}
  • To add new repository search method (let's say search by criteria), define new repository interface like IEmployeeRepository : IRepository<Employee>, add the new method and call SharepointRepositoryBase method:
protected List<T> GetItemsByQuery(string camlQueryString)

  • In order to implement different actions, see memory-leak safe, auto disposing wrapper for SPWeb, SPSite pair: AutoDisposeWebSite with code comments

Last edited Apr 25, 2011 at 6:54 AM by Extreme, version 15

Comments

No comments yet.