AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Monday 21 December 2015

Repository Pattern in c#

Repository Pattern separates the data access logic and maps it to the entities in the business logic. It works is to reduce complexity & increase code re-usability. It allows you to write unit tests instead of integration tests. It hides the details of data access from the business logic. In other words, business logic can access the data object without having knowledge of the underlying data access architecture.


Repository Pattern can be implemented using two ways.

Non-Generic

In non-generic repository pattern each entity have its repository class. Lets take example if their is two entity classes like Customer & Employee then we need to create two different repository.

Generic

A generic repository class is used for all entity operations like insert, update & delete by single class.

Example of generic repository

Step 1- Create new interface IGenericRepository


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();
   }

Step 2- Add new class GenericRepository
public 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(System.Linq.Expressions.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();
      }
  }

Example how to call generic pattern.

public static List<DesignationEntity> GetDesignation(int DesignationId)
       {
           List rType = null;
           IGenericRepository<Designation> _repository = new GenericRepository<HRMEntities, Designation>();
           try
           {
               rType = _repository.FindBy(m => m.DesignationId == DesignationId || DesignationId == 0).Select(m => new DesignationEntity
               {
                   DesignationId = m.DesignationId,
                   DesignationName = m.DesignationName
               }).ToList();
           }
           catch
           {
               throw;
           }
           return rType;
       }

In this example getting list of Designation by designationid.

0 comments :

Post a Comment

  • Popular Posts
  • Comments