The Repository Pattern.

The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD).

Repository pattern.

The Repository Design PatternĀ is one of the most useful and most widely applicable design patterns ever invented. Any application has to work with persistence and with some kind of list of items. These can be users, products, networks, disks, or whatever your application is about. If you have a blog for example, you have to deal with lists of blog posts and lists of comments. The problem that all of these list management logic have in common is how to connect business logic, factories and persistence.

So let’s create the base generic repository interface first.

public interface IGenericRepository<T> where T : class
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Delete(T entity);
    void Edit(T entity);
    void Save();
}

Then we create an abstract class which implementsĀ this interface and also accepts another type parameter defined in a generic declaration which is a type of DbConetxt class:

public abstract class GenericRepository<C, T> :IGenericRepository<T> where T : class where C : DbContext, new()
{
    private C _entities = new C();
    public C Context
    {

        get { return _entities; }
        set { _entities = value; }
    }

    public virtual IQueryable<T> GetAll()
    {

        IQueryable<T> query = _entities.Set<T>();
        return query;
    }

    public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
    {

        IQueryable<T> query = _entities.Set<T>().Where(predicate);
        return query;
    }

    public virtual void Add(T entity)
    {
        _entities.Set<T>().Add(entity);
    }

    public virtual void Delete(T entity)
    {
        _entities.Set<T>().Remove(entity);
    }

    public virtual void Edit(T entity)
    {
        _entities.Entry(entity).State = EntityState.Modified;
    }

    public virtual void Save()
    {
        _entities.SaveChanges();
    }

    private bool disposed = false;
    protected virtual void Dispose(bool disposing)
    {

        if (!this.disposed)
            if (disposing)
                _entities.Dispose();

        this.disposed = true;
    }

    public void Dispose()
    {

        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Now we have abstract class that implements basic and ordinary methods. Each of these methods can be overriden if necessary.

After this, we can implement concrete repository class:

public class PlayerRespository :
    GenericRepository<OurEntities, Player>, IPlayerRepository
{
    public Player GetSingle(int playerId)
    {
        var query = GetAll().FirstOrDefault(x => x.playerId == playerId);
        return query;
    }
}
Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this