Monday, 8 April 2019

Generic EntityFramework Repository Pattern

First You need to create an interface 
called IRepository.cs

  1. Create IRepository for call generic methods


  public interface IRepository : IDisposable
    {
        TItem Update<TItem>(TItem item) where TItem : class ,new();

        TItem Save<TItem>(TItem item) where TItem : class ,new();
        TItem Delete<TItem>(TItem item) where TItem : class ,new();

        void Save();

        IEnumerable<T> Select<T>() where T : class;

        IEnumerable<TItem> FindAllBy<TItem>(System.Linq.Expressions.Expression<Func<TItem, bool>> predicate) where TItem : class ,new();


        TItem FindFirstOrDefaultBy<TItem>(System.Linq.Expressions.Expression<Func<TItem, bool>> predicate) where TItem : class ,new();

        IEnumerable<TItem> FindAll<TItem>() where TItem : class,new();
    }

  2. Implement Repository

public abstract  class Repository<TContext> : IRepository
       where TContext : DbContext ,new ()
    {
       private readonly TContext _context;

       public TContext Context { get { return _context; } }

       protected Repository()
       {
           _context = new TContext(); 
       }

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


       public TItem Update<TItem>(TItem item) where TItem : class ,new()
       {
           return PerformAction(item, EntityState.Modified);

       }

       public TItem Delete<TItem>(TItem item) where TItem : class ,new()
       {
           _context.Entry(item).State = EntityState.Deleted;
           return PerformAction(item, EntityState.Modified);

       }

       public TItem Update<TItem>(TItem item) where TItem : class ,new()
       {
           return PerformAction(item, EntityState.Added);

       }
       public void Save()
       {
           _context.SaveChanges();
       }


       public IEnumerable<T> Select<T>() where T : class
       {
           return Context.Set<T>();
       }


       protected virtual TItem PerformAction<TItem>(TItem item, EntityState entitystate) where TItem : class,new()
       {
           _context.Entry(item).State = entitystate;
           _context.SaveChanges();
           return item;
       }


       public IEnumerable<TItem> FindAllBy<TItem>(System.Linq.Expressions.Expression<Func<TItem, bool>> predicate) where TItem : class,new()
       {
           IEnumerable<TItem> query = Context.Set<TItem>().Where(predicate).ToList();
       }


       public TItem FindFirstOrDefaultBy<TItem>(System.Linq.Expressions.Expression<Func<TItem, bool>> predicate) where TItem : class,new()
       {
           TItem query = Context.Set<TItem>().Where(predicate).ToList();
           return query;
       }

       public IEnumerable<TItem> FindAll<TItem>(System.Linq.Expressions.Expression<Func<TItem, bool>> predicate) where TItem : class,new()
       {
           IEnumerable<TItem> query = Context.Set<TItem>().ToList();
       }


    }


3.Inherit that repository in DAL
 public class EmployeeRepository : Repository<PMSEntities>, IEmployee


4.Define implementation in container file





No comments:

Post a Comment