Orderly list return at BLL?

Asked

Viewed 47 times

1

In my project I use the EF6, use generic repositories and in my layer BLL I have the class ClienteBLL, which is a class that has the logical implementation methods of the class.

In one of the methods I return a list that receives all customers from the bank and then make an ordering

Method:

public IList<ClienteDTO> TodosClientes()
{
   List<ClienteDTO> lista = clienteRepo.BuscarTodos().ToList();    
   lista.Sort(delegate (ClienteDTO a, ClienteDTO b) { return a.nome.CompareTo(b.nome); });

   return lista;
}

My doubt: Is it wrong to order the list that way? If so, what do you recommend?

I found a much better way: How to Sort a List by a Property in the Object

So based on the link response I did so:

public IList<ClienteDTO> TodosClientes()
{
    return clienteRepo.BuscarTodos().OrderBy(o => o.nome).ToList();
}

Now I have no doubt it’s right!

My generic repository:

public abstract class GenericRepository<T> : IDisposable, IGenericRepository<T> where T : class
    {
        GdPContext contexto = new GdPContext();


        public IQueryable<T> BuscarTodos()
        {
            IQueryable<T> query = contexto.Set<T>();
            return query;
        }

        public IQueryable<T> Buscar(Expression<Func<T, bool>> predicado)
        {
            IQueryable<T> query = contexto.Set<T>().Where(predicado);
            return query;
        }

        public void Adicionar(T entity)
        {
            contexto.Set<T>().Add(entity);
        }

        public void Excluir(T entity)
        {
            contexto.Set<T>().Remove(entity);
        }

        public void Editar(T entity)
        {
            contexto.Set<T>().AddOrUpdate(entity);
        }

        public void Salvar()
        {
            contexto.SaveChanges();
        }

        public void Dispose()
        {
            contexto.Dispose();
        }
    }
  • 1

    The two ways you’re using are not recommended, because, you’re doing it wrong, you should do the OrderBy in a generic method in your repository class and use to sort in your SQL, what is happening is that you materialize the information and then sort in memory this causes low performance.

  • Friend, if it is not asking a lot you have to explain better the part of ordering in SQL

  • 1

    I need the implementation of your Repository, because then I can explain what could be done. In the explanation of SQL is why you before materializing the data (ToList(), First(), etc) could add Orderby so that in your SQL is present the ordering and with the I reported not in memory.

  • 1

    Got it! I edited the question and put the generic repository

1 answer

1


In the implementation of its interface (IGenericRepository<T>) create another method with the following signature:

IQueryable<T> BuscarTodos<Tkey>(Expression<Func<T,Tkey>> orderBy);

and soon after implementing in your class GenericRepository<T>, within the method make the following code which is ideal and the ordering made direct in the instruction SQL and the orderly data coming from your table:

public IQueryable<T> BuscarTodos<Tkey>(Expression<Func<T, Tkey>> orderBy)
{
    return contexto.Set<T>()
                   .AsNoTracking()
                   .OrderBy(orderBy); 
}

To use this code:

clienteRepo.BuscarTodos(o => o.nome).ToList();

The method that has been added AsNoTracking() is more an optimization, where the returned data will only be read and not kept in context, IE, the data is read only, this is good to show data that will not be changed at this time.

  • 1

    Man, thank you so much! This will help me so much from now on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.