
Download Project (Full Project)
Step-1
In this Part i will show how to create repository pattern in eCommerce project. Go to visual Studio 2015 and Open Same Project where we are working, Go to solution explorer create a folder name as Repository, in this folder create class name as Repository. Class change by Interface. inheritance by the class. Then the Interface define method are must and should implemented into class. This repository class is communicating the database by the dataset. This method responsible to all necessary operation is done with database. Given bellow IRepository.cs source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
namespace OnlineShoppingStore.Repository
{
public interface IRepository<Tbl_Entity> where Tbl_Entity:class
{
IEnumerable<Tbl_Entity> GetAllRecords();
IQueryable<Tbl_Entity> GetAllRecordsIQueryable();
int GetAllrecordCount();
void Add(Tbl_Entity entity);
void Update(Tbl_Entity entity);
void UpdateByWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict, Action<Tbl_Entity> ForEachPredict);
Tbl_Entity GetFirstorDefault(int recordId);
void Remove(Tbl_Entity entity);
void RemovebyWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict);
void RemoveRangeBywhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict);
void InactiveAndDeleteMarkByWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict, Action<Tbl_Entity> ForEachPredict);
Tbl_Entity GetFirstorDefaultByParameter(Expression<Func<Tbl_Entity, bool>> wherePredict);
IEnumerable<Tbl_Entity> GetListParameter(Expression<Func<Tbl_Entity, bool>> wherePredict);
IEnumerable<Tbl_Entity> GetResultBySqlprocedure(string query, params object[] parameters);
IEnumerable<Tbl_Entity> GetRecordsToShow(int PageNo, int PageSize, int CurrentPage, Expression<Func<Tbl_Entity, bool>> wherePredict, Expression<Func<Tbl_Entity, int>> orderByPredict);
}
}
Step-2
Now add new Clase in Same folder name as “GenericRepository”. This repository file is passing the value into Repository file and receives the result. Given below source code:
using OnlineShoppingStore.DAL;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
namespace OnlineShoppingStore.Repository
{
public class GenericRepository<Tbl_Entity> : IRepository<Tbl_Entity> where Tbl_Entity : class
{
DbSet<Tbl_Entity> _dbSet;
private dbMyOnlineShoppingEntities _DBEntity;
public GenericRepository(dbMyOnlineShoppingEntities DBEntity)
{
_DBEntity = DBEntity;
_dbSet = _DBEntity.Set<Tbl_Entity>();
}
public void Add(Tbl_Entity entity)
{
_dbSet.Add(entity);
_DBEntity.SaveChanges();
}
public int GetAllrecordCount()
{
return _dbSet.Count();
}
public IEnumerable<Tbl_Entity> GetAllRecords()
{
return _dbSet.ToList();
}
public IQueryable<Tbl_Entity> GetAllRecordsIQueryable()
{
return _dbSet;
}
public Tbl_Entity GetFirstorDefault(int recordId)
{
return _dbSet.Find(recordId);
}
public Tbl_Entity GetFirstorDefaultByParameter(Expression<Func<Tbl_Entity, bool>> wherePredict)
{
return _dbSet.Where(wherePredict).FirstOrDefault();
}
public IEnumerable<Tbl_Entity> GetListParameter(Expression<Func<Tbl_Entity, bool>> wherePredict)
{
return _dbSet.Where(wherePredict).ToList();
}
public IEnumerable<Tbl_Entity> GetResultBySqlprocedure(string query, params object[] parameters)
{
if (parameters != null)
{
return _DBEntity.Database.SqlQuery<Tbl_Entity>(query, parameters).ToList();
}
else
return _DBEntity.Database.SqlQuery<Tbl_Entity>(query).ToList();
}
public void InactiveAndDeleteMarkByWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict, Action<Tbl_Entity> ForEachPredict)
{
_dbSet.Where(wherePredict).ToList().ForEach(ForEachPredict);
}
public void Remove(Tbl_Entity entity)
{
if (_DBEntity.Entry(entity).State == EntityState.Detached)
_dbSet.Attach(entity);
_dbSet.Remove(entity);
}
public void RemovebyWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict)
{
Tbl_Entity entity = _dbSet.Where(wherePredict).FirstOrDefault();
Remove(entity);
}
public void RemoveRangeBywhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict)
{
List<Tbl_Entity> entity = _dbSet.Where(wherePredict).ToList();
foreach(var ent in entity)
{
Remove(ent);
}
}
public void Update(Tbl_Entity entity)
{
_dbSet.Attach(entity);
_DBEntity.Entry(entity).State = EntityState.Modified;
}
public void UpdateByWhereClause(Expression<Func<Tbl_Entity, bool>> wherePredict, Action<Tbl_Entity> ForEachPredict)
{
_dbSet.Where(wherePredict).ToList().ForEach(ForEachPredict);
}
public IEnumerable<Tbl_Entity> GetRecordsToShow(int PageNo, int PageSize, int CurrentPage, Expression<Func<Tbl_Entity, bool>> wherePredict, Expression<Func<Tbl_Entity, int>> orderByPredict)
{
if(wherePredict!=null)
{
return _dbSet.OrderBy(orderByPredict).Where(wherePredict).ToList();
}
else
{
return _dbSet.OrderBy(orderByPredict).ToList();
}
}
}
}
Step-3
In this folder create new class name as ” GenericUnitOfWork” Now create the object this class "dbMyOnlineShoppingEntities" name as "DBEntity". SaveChanges() this method is responsible to DBEntity save and Dispose() method is responsible to DBEntity Dispose. Given bellow the code :
using OnlineShoppingStore.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OnlineShoppingStore.Repository
{
public class GenericUnitOfWork:IDisposable
{
private dbMyOnlineShoppingEntities DBEntity = new dbMyOnlineShoppingEntities();
public IRepository<Tbl_EntityType> GetRepositoryInstance<Tbl_EntityType>() where Tbl_EntityType : class
{
return new GenericRepository<Tbl_EntityType>(DBEntity);
}
public void SaveChanges()
{
DBEntity.SaveChanges();
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
DBEntity.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool disposed = false;
}
}
Step-4
Now save the file. (Ctrl+S)