Friday 1 May 2015

Revised : Generic data access for repository pattern


public class GenericDAL<T> : IDisposable
        where T : class
    {
        private EDMX.RMSModel model;
        private DbSet<T> dbSet;
        public GenericDAL()
        {
            model = new RMSModel();
            dbSet = model.Set<T>();
        }

        // Pass order by fields. form multiple fields pass fields by Comma(;) separated
         public IEnumerable<T> GetList(Expression<Func<T, bool>> where = null,
                                                        string orderBy = null,
                                                        SortDirection sortDir = SortDirection.Ascending)
        {
            IQueryable<T> query = dbSet;
            if (where != null)
            {
                query = query.Where(where);
            }

            if (!string.IsNullOrWhiteSpace(orderBy))
            {
                var orderFields = orderBy.Split(';');
                return OrderByDynamic(query, orderFields, sortDir);
            }
            else
            {
                return query.ToList();
            }
        }

        public T GetDetails(Expression<Func<T, bool>> where)
        {
            if (where != null)
            {
                IQueryable<T> query = dbSet;
                return query.Where(where).FirstOrDefault();
            }

            return null;
        }

        public int Insert(T entity)
        {
            dbSet.Add(entity);
            return model.SaveChanges();
        }

        public int Update(T entity)
        {
            dbSet.Attach(entity);
            model.Entry(entity).State = EntityState.Modified;
            return model.SaveChanges();
        }

        public int Delete(Expression<Func<T, bool>> where)
        {
            IQueryable<T> query = dbSet;
            T entity = query.Where(where).FirstOrDefault();
            dbSet.Remove(entity);
            return model.SaveChanges();
        }

        private List<T> OrderByDynamic(IQueryable<T> query = null, string[] sortExpressions = null,
                                                         SortDirection direction = SortDirection.Ascending)
        {
            // No sorting needed
            if ((sortExpressions == null) || (sortExpressions.Length <= 0))
            {
                return query.ToList();
            }

            // Let us sort it
            IOrderedEnumerable<T> orderedQuery = null;
            for (int i = 0; i < sortExpressions.Length; i++)
            {
                var index = i;
                Func<T, object> expression = item => item.GetType()
                                .GetProperty(sortExpressions[index])
                                .GetValue(item, null);

                if (direction == SortDirection.Ascending)
                {
                    orderedQuery = (index == 0) ?
                                              query.OrderBy(expression):
                                              orderedQuery.ThenBy(expression);
                }
                else
                {
                    orderedQuery = (index == 0) ?
                                              query.OrderByDescending(expression):
                                              orderedQuery.ThenByDescending(expression);
                }
            }

            return orderedQuery.ToList();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                model.Dispose();
            }

            this.disposed = true;
        }

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

    }

No comments:

Post a Comment