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





Friday, 4 August 2017

LINQ CRUD Operation

First Step : Drag Your All database table in Linq File Following Are Step
  • Right Click On Project
  • Add New Item
  • Select Linq File
  • Drag your Data Table
  • Save and Buit A project
Then Declare Namespace for The Linq File

Click on Image to Show in Details


1)How To Write a Insert/update Code

Click on Image to Show in Details

In Above Example Vender_Event is Table
To Save you have to just to assign all object of Table and in update you have to pass table primary key before submit.

2)Delete code

Click on Image to Show in Details

In Above Code student is table and we fetch the student data by its ID.




Thank you , Hope you like this Operation details.