Tuesday 1 April 2014

Use of Generic with Repository pattern

Hello All,
          Here is an example for generic repository pattern.  Now what is Generic ?  And the answer is common called generic. Yes .net provide generic by same way. You can create a generic class and it will be independent from datatype. Means, Your datatype will desided by your code to use generic. You can see in following example.  Where  T = Generic Datatype. It will same for whole class when you create object of the class.

Let’s see the Generic class and make it in use.

Generic Class :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace Web_test
{
    public class GenericRepository<T>
        where T : class
    {
        internal MyDB objMyDB;  //  MyDB = Datacontext  in Linq to Entity
        internal DbSet<T> dbSet;
        public GenericRepository(MyDB objMyDB)
        {
            this.objMyDB = objMyDB;
            dbSet = objMyDB.Set<T>();
        }

        public IEnumerable<T> GetList(Expression<Func<T, bool>> filter = null,
            Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
        {
            IQueryable<T> query = dbSet;
            if (filter != null)
            {
                query = query .Where(filter);
            }
            foreach (var includeProperty in includeProperties.Split(new char[] { ',' },
                        StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }
            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public T GetDetailByID (object ID)
        {
            return dbSet.Find(ID);           
        }

        public void Insert (T entity)
        {
            dbSet.Add(entity);
        }

        public void Delete (int ID)
        {
            T entityToDelete = dbSet.Find(ID);
            dbSet.Remove(entityToDelete);           
        }

        public void Update(T entity)
        {
            dbSet.Attach(entity);
            objMyDB.Entry(entity).State = EntityState.Modified;
        }

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

        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    objMyDB.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}


We are using Linq to Entity framework for repository. Now Let’s see how only one class can do all operations for all entities.



using System.Collections.Generic;
using System.Linq;

namespace Web_test
{
    public class CRUDOperations
    {
        private MyDB objDB = new MyDB();
        private GenericRepository<tblInfo> objInfo;
        public void Insert()
        {
            tblInfo objInfo = new tblInfo();    // We have table with name = tblInfo
            objInfo.City = 1;
            objInfo.Contact = "7777978360";
            objInfo.Name = "Ajay Patel";
            objInfoProp.Insert(objInfo);
            objInfoProp.save();
        }
        public void Update()
        {
            tblInfo objInfo = objInfoProp.GetDetailByID(1);   // We have table with name = tblInfo
            objInfo.City = 2;
            objInfo.Contact = "7777978360";
            objInfo.Name = "AP";
            objInfoProp.Update(objInfo);
        }
        public void Delete()
        {
            objInfoProp.Delete(1);
        }
        public void GetList()
        {
            List<tblInfo> lstInfo = objInfoProp.GetList(p => p.Name == "AP").ToList();
        }

        public GenericRepository<tblInfo> objInfoProp
        {
            get
            {
                if (objInfo == null)
                {
                    objInfo = new GenericRepository<tblInfo>(objDB);
                }
                return objInfo;
            }
        }
    }
}




This is the use of generic. I am not good to explain but hope you all have got this if not then contact me

Thanks

No comments:

Post a Comment