locked
Evaluate my function please (EF - repository pattern) RRS feed

  • Question

  • User-1669453032 posted

    Hi,

    i am using EF 6.1.3 with mvc 5. 

    i am using it through repository pattern.

    i want to know either it is excellent if we evaluate the performance.

    i want to get the data page wise so i want that this will just query for one page from a database and pick just 15 records on a single page.

    Evaluate my function please.

      public virtual IEnumerable<TEntity> GetSorted<T>(Expression<Func<TEntity, T>> order, int skip, int take, string orderType,
              Func<TEntity, bool> search, params Expression<Func<TEntity, object>>[] includes)
            {
    
                IQueryable<TEntity> query = dbSet;
    
                foreach (var include in includes)
                {
                    query = dbSet.Include(include);
                }
    
                IEnumerable<TEntity> data = null;
                if (!string.IsNullOrEmpty(orderType) && orderType.ToLower().Trim() == "asc")
                {
                    data = query.OrderBy(order).Skip(skip).Take(take).Where(search).ToList();
                }
                else if (!string.IsNullOrEmpty(orderType) && orderType.ToLower().Trim() == "desc")
                {
                    data = query.OrderByDescending(order).Skip(skip).Take(take).Where(search).ToList();
                }
                else
                {
                    // in case order type was NULL
                    data = query.OrderBy(order).Skip(skip).Take(take).ToList();
                }
    
    
    
                return data;
            }

    here i am calling through it.

      Expression<Func<HeadOffice, string>> orderByFunc = o => o.Code;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();

    Tuesday, December 8, 2015 12:36 PM

Answers

  • User-271186128 posted

    Hi softech.employee,

                    if (options.sort == "Id")
                    {
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int> (orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Name")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Name;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Code")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Code;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else
                    {
                        // default sort.
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
    

    As for the multiple if else if clause, I suppose you could try to use switch case clause. For more details about the difference between IF Else and Switch case, you could refer to the following articles:

    https://www.quora.com/What-is-the-difference-between-switch-and-if-else-statements-Which-is-better-in-different-cases-Why

    http://www.codewithc.com/difference-between-switch-case-and-else-if-ladder/

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2015 5:49 AM

All replies

  • User-1669453032 posted

    this is the complete repository class 

    ===============================================================

      public class GenericRepository<TEntity> where TEntity : class
        {
            private SmsContext context;
            private DbSet<TEntity> dbSet;
    
            public GenericRepository(SmsContext context)
            {
                this.context = context;
                this.context.Configuration.ProxyCreationEnabled = false;
                this.dbSet = context.Set<TEntity>();
            }
    
            public virtual IEnumerable<TEntity> Get(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = "")
            {
                IQueryable<TEntity> 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 virtual IEnumerable<TEntity> Get(params Expression<Func<TEntity, object>>[] includes)
            {
    
                IQueryable<TEntity> query = dbSet;
    
    
                foreach (var include in includes)
                {
                    query = dbSet.Include(include);
                }
    
                return query.ToList();
            }
    
            // public virtual IEnumerable<TEntity> GetSorted(Func<TEntity, Object> order,int skip, int take, params Expression<Func<TEntity, object>>[] includes)
            public virtual IEnumerable<TEntity> GetSorted<T>(Expression<Func<TEntity, T>> order, int skip, int take, string orderType,
              Func<TEntity, bool> search, params Expression<Func<TEntity, object>>[] includes)
            {
    
                IQueryable<TEntity> query = dbSet;
    
                foreach (var include in includes)
                {
                    query = dbSet.Include(include);
                }
    
                IEnumerable<TEntity> data = null;
                if (!string.IsNullOrEmpty(orderType) && orderType.ToLower().Trim() == "asc")
                {
                    data = query.OrderBy(order).Skip(skip).Take(take).Where(search).ToList();
                }
                else if (!string.IsNullOrEmpty(orderType) && orderType.ToLower().Trim() == "desc")
                {
                    data = query.OrderByDescending(order).Skip(skip).Take(take).Where(search).ToList();
                }
                else
                {
                    // in case order type was NULL
                    data = query.OrderBy(order).Skip(skip).Take(take).ToList();
                }
    
    
    
                return data;
            }
    
            public virtual int Count()
            {
                IQueryable<TEntity> query = dbSet;
    
                int data = dbSet.Count<TEntity>();
    
                return data;
            }
    
            public virtual TEntity GetById(object id)
            {
                return dbSet.Find(id);
            }
    
            public virtual void Insert(TEntity entity)
            {
                dbSet.Add(entity);
            }
    
            public virtual void Delete(object id)
            {
                TEntity entityToDelete = dbSet.Find(id);
                Delete(entityToDelete);
            }
    
            public virtual void Delete(TEntity entityToDelete)
            {
                if (context.Entry(entityToDelete).State == EntityState.Detached)
                {
                    dbSet.Attach(entityToDelete);
                }
                dbSet.Remove(entityToDelete);
            }
    
            public virtual void Update(TEntity entityToUpdate)
            {
                dbSet.Attach(entityToUpdate);
                context.Entry(entityToUpdate).State = EntityState.Modified;
            }
        }

    and this is the complete call of this function 

      public List<HeadOffice> GeAlltHeadOffices(TblOptions options)
            {
                try
                {
                    List<HeadOffice> ho = new List<HeadOffice>();
                    Func<HeadOffice, bool> search = x=> 1==1;
    
                    if (!string.IsNullOrEmpty(options.search))
                    {
                        search = x => x.Id.ToString() == options.search || x.Name.ToLower().Contains(options.search.ToLower()) || x.Code.ToLower().Contains(options.search.ToLower());
                    }
    
                    if (options.sort == "Id")
                    {
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int> (orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Name")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Name;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Code")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Code;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else
                    {
                        // default sort.
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    
                    return ho;
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
            }

    Please improve it (evaluate it) or guide me in a better ways . 

    thanks 

    Tuesday, December 8, 2015 12:56 PM
  • User-271186128 posted

    Hi softech.employee,

                    if (options.sort == "Id")
                    {
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int> (orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Name")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Name;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else if (options.sort == "Code")
                    {
                        Expression<Func<HeadOffice, string>> orderByFunc = o => o.Code;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<string>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
                    else
                    {
                        // default sort.
                        Expression<Func<HeadOffice, int>> orderByFunc = o => o.Id;
                        ho = unitOfWork.HeadOfficeRepository.GetSorted<int>(orderByFunc, options.offset, options.limit, options.order, search, x => x.Address, x => x.ContactInfo, x => x.ContactPerson).ToList();
                    }
    

    As for the multiple if else if clause, I suppose you could try to use switch case clause. For more details about the difference between IF Else and Switch case, you could refer to the following articles:

    https://www.quora.com/What-is-the-difference-between-switch-and-if-else-statements-Which-is-better-in-different-cases-Why

    http://www.codewithc.com/difference-between-switch-case-and-else-if-ladder/

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2015 5:49 AM