Introduction to IRepository

The IRepository is a generic interface and it has T is a BaseEntity. It's contained in namespace Aaron.Core.Data. It's very important, and is considered the lifeline of Aaron systems.

public interface IRepository<T> where T : BaseEntity
The IRepository got 8 functions to support basic database accessing:

/// <summary>
/// Inserts the specified obj.
/// </summary>
/// <param name="obj">The object to insert.</param>
void Insert(T obj);

/// <summary>
/// Updates the specified obj.
/// </summary>
/// <param name="obj">The object to delete.</param>
void Update(T obj);

/// <summary>
/// Deletes the specified id.
/// </summary>
/// <param name="id">The identity of object to delete.</param>
void Delete(object id);

/// <summary>
/// Deletes the specified obj.
/// </summary>
/// <param name="obj">The object to delete.</param>
void Delete(T obj);

/// <summary>
/// Deletes the specified data.
/// </summary>
/// <param name="data">The data is a specified list from database.</param>
void Delete(IQueryable<T> data);

/// <summary>
/// Gets the specified filter.
/// </summary>
/// <param name="filter">The filter for specified list.</param>
/// <param name="skip">The skip of specified row.</param>
/// <param name="take">The records want be taken.</param>
/// <param name="orderBy">The order by.</param>
/// <param name="includeProperties">The include properties to join many tables.</param>
/// <returns></returns>
IQueryable<T> Get(Expression<Func<T, bool>> filter = null,
    int skip = 0,
    int take = 0,
    Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
    string includeProperties = null);

/// <summary>
/// Gets the by id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
T GetById(object id);

/// <summary>
/// Gets the table.
/// </summary>
IQueryable<T> Table { get; }
In Aaron system, the IRepository is bound to ImplRepository class. This class is used to define 8 functions, and it's as a bridge between BaseEntity to database. And to use the IRepositoy, you just do as follows:
  • Declare a field scope is private, and get a similar attribute from constructor:
private readonly IRepository<Article> _articleRepository;

// The ctor of Example class.
public Example(IRepository<Article> articleRepository)
    _articleRepository = articleRepository;
  • Or use the IoC.Resolve<T>() method to call IRepository, it's contained Aaron.Core.Infrastructure:
var articleRepository = IoC.Resolve<IRepository<Article>>();
Now, you can use 8 functions to access BaseEntity object to database. But, you should notice that: at first case, the Example class must be bound to Container, to become a DependencyResolver.

I hope this article helpful for you. Thanks for the reference!

Last edited Oct 24, 2012 at 4:21 PM by khiemnd777, version 7


No comments yet.