List or Datatable desktop c # [closed]

0

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

When the UI layer requests multiple records, it would be best to load everything inside a < > or use the datatable?

Today I use the following form:

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();
    }
}
    
asked by anonymous 24.04.2016 / 22:39

1 answer

1

Even though the edition was still very ambiguous, its idea of "better", I will mention some interesting points that may end up covering your doubt.

Thinking about performance, a lot of people say that List has a better performance than DataTable, and since the goal is simply to send it to the UI, the List class of the message account without 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 talks about 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 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 transform 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 have answered your question.

    
25.04.2016 / 05:42