adrianvlupu
4/9/2014 - 8:09 AM

Generic repository for EF

Generic repository for EF

using OrangeMSE.Business;
using OrangeMSE.Business.Data;
using OrangeMSE.Business.Helpers;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;

public abstract class Repository<T> : IDisposable, IRepository<T> where T : class
{
    internal DbContext context { get; private set; }
    protected DbSet<T> objectSet;


    internal Repository(bool ProxyCreationEnabled = true)
        : this(new Entities())
    {
        ((IObjectContextAdapter)context).ObjectContext.ContextOptions.ProxyCreationEnabled = ProxyCreationEnabled;
    }
    internal Repository(DbContext _context)
    {
        context = _context;
        objectSet = context.Set<T>();
    }

    public virtual IQueryable<T> GetAll()
    {
        return objectSet;
    }

    public virtual IQueryable<T> GetAll(Expression<Func<T, bool>> predicate)
    {
        return predicate == null ? objectSet : objectSet.Where<T>(predicate);
    }

    public virtual T Get(Expression<Func<T, bool>> predicate)
    {
        return objectSet.Where<T>(predicate).FirstOrDefault();
    }

    public virtual K Add<K>(T entity, Expression<Func<T, object>> property)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.Add(entity);
        context.SaveChanges();

        var propertyName = GeneralExtensions.GetPropertyName<T, object>(property);
        return (K)entity.GetType().GetProperty(propertyName).GetValue(entity, null);
    }

    public virtual T Add(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.Add(entity);
        context.SaveChanges();

        return entity;
    }

    public virtual void AddRange(List<T> entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.AddRange(entity);
        context.SaveChanges();
    }

    public virtual void Update(T entity)
    {
        objectSet.Attach(entity);
        context.Entry<T>(entity).State = EntityState.Modified;
        context.SaveChanges();
    }

    public virtual void Update(T entity, params Expression<Func<T, object>>[] modifiedProperties)
    {
        objectSet.Attach(entity);
        context.Configuration.ValidateOnSaveEnabled = false;

        foreach (var property in modifiedProperties)
        {
            string propertyName = GeneralExtensions.GetPropertyName<T, object>(property);
            context.Entry<T>(entity).Property(propertyName).IsModified = true;
        }

        context.SaveChanges();
        context.Configuration.ValidateOnSaveEnabled = true;
    }

    public virtual void Delete(Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> records = from x in objectSet.Where<T>(predicate) select x;
        foreach (T record in records)
        {
            objectSet.Remove(record);
        }

        context.SaveChanges();
    }

    public void Dispose()
    {
        context.Dispose();
    }
}