List or Datatable desktop c#

Asked

Viewed 439 times

0

I have an application used layers and I have a question...

When the UI layer requests multiple records, it would be better to load the whole into a list<> or use the datatable?

Today I use it as follows:

public List<ClienteModel> Listagem(string filtro)
{
    try
    {
        AbrirConexao();
        if (filtro == "")
        {
            Cmd = new SqlCommand("Select * from Clientes INNER JOIN Estados ON Clientes.id_uf = Estados.Id", Con);
        }
        else
        {
            //Usando Filtro - A Implementar
        }
        Dr = Cmd.ExecuteReader();
        List<ClienteModel> lista = new List<ClienteModel>();
        while (Dr.Read())
        {
            ClienteModel c = new ClienteModel();
            c.Id = Convert.ToInt32(Dr["id"]);
            c.CodigoCliente = Convert.ToInt32(Dr["codcli"]);
            c.Nome = Convert.ToString(Dr["nome"]);
            c.Endereco = Convert.ToString(Dr["endereco"]);
            c.NumeroEndereco = Convert.ToString(Dr["nr"]);
            c.Bairro = Convert.ToString(Dr["bairro"]);
            c.Cidade = Convert.ToString(Dr["cidade"]);
            c.Cep = Convert.ToString(Dr["cep"]);
            c.Estado.id = Convert.ToInt32(Dr["id_uf"]); 
            c.Estado.sigla = Convert.ToString(Dr["sigla"]);
            c.Observacoes = Convert.ToString(Dr["obs"]);
            lista.Add(c);
        }
        return lista;
    }   
    catch (Exception ex)
    {
        throw new Exception("Erro na Listagem dos Clientes.. " + ex.Message);
    }
    finally
    {
        FecharConexao();
    }
}
  • How about you ask what the difference is between the two ? " what would be better" fits into a question based on opinion what and outside the scope of the site understands ? I can post an example showing both ways and you can compare and define which is best for your case, but cool and you remove your question from the format based on opinions ,it can be negative or closed >.<

  • No context has no answer. Both can be very bad depending on the use.

  • I edited the post.. I was on Cellular and did not have the code.. is more a matter of what would be better even..

1 answer

1

Even with the edition still very ambiguous his idea of "better", I will quote some interesting points that can end up covering your doubt.

Thinking about performance, a lot of people say that List performs better than Datatable, and since the goal is simply to send it to the UI, the Message Account List class with no major problems.

Another thing worth mentioning, there is a concept in the area of Object Orientation called SOLID, the fifth and last of these concepts speaks of Dependency Inversion Principle, in this principle it is commented that you should not use implementation, but abstraction in your code. You could use an Ilist to store these values.

Note that the Ilist receives the Icollection and Ienumerable, you can also use one of the two if you deem it necessary.

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

Understood this, you can use the . Tolist() method or any other conversion method to turn into an object collection:

IList<ClienteModel> lista = new IList<ClienteModel>;
            while (Dr.Read())
            {
                ClienteModel c = new ClienteModel();
                c.Id = Convert.ToInt32(Dr["id"]);
                c.CodigoCliente = Convert.ToInt32(Dr["codcli"]);
                c.Nome = Convert.ToString(Dr["nome"]);
                c.Endereco = Convert.ToString(Dr["endereco"]);
                c.NumeroEndereco = Convert.ToString(Dr["nr"]);
                c.Bairro = Convert.ToString(Dr["bairro"]);
                c.Cidade = Convert.ToString(Dr["cidade"]);
                c.Cep = Convert.ToString(Dr["cep"]);
                c.Estado.id = Convert.ToInt32(Dr["id_uf"]);
                c.Estado.sigla = Convert.ToString(Dr["sigla"]);
                c.Observacoes = Convert.ToString(Dr["obs"]);
                lista.Add(c);
            }
            return lista.ToList();

I hope I answered your question.

Browser other questions tagged

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