How to optimize a class so that I can return a list or an entity

0

Dear All.

I am new to the platform c # I am developing an application in 3 layers, in the business logic layer I make a client query which brings me a list and I fill it with a datagridview according to a search criteria, examples by names or by identification number or by customer code or even by addresses.

Here the code

    public List<ECliente> GetListClientes(string Criterio)
    {
        List<ECliente> lista = new List<ECliente>();
        IDataReader reader = null;
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT");
        sb.Append(" CLI.CLI_CODIGO ");
        sb.Append(", CLI.CLI_NOMBRE");
        sb.Append(", CLI.CLI_RUC");
        ...
        ...
        ...
        sb.Append(", CLI.TP_CODIGO ");
        sb.Append("FROM CLIENTES CLI, GRUPOS GRU ");
        sb.Append("WHERE CLI.GRU_CODIGO = GRU.GRU_CODIGO ");
        if (String.IsNullOrEmpty(Criterio))
        {
            // Todos los Clientes
        }
        else
        {
            sb.Append(Criterio);
        }



        try
        {
            conexion.Open();

            reader = conexion.ExecuteReader(CommandType.Text, sb.ToString());

            while (reader.Read())
            {

                ECliente eCliente = new ECliente();
                eCliente.CLI_CODIGO = (Int32)reader["CLI_CODIGO"];
                eCliente.CLI_NOMBRE = reader["CLI_NOMBRE"].ToString().Trim();
                eCliente.CLI_RUC = reader["CLI_RUC"].ToString().Trim();
                eCliente.CLI_DIRECCION = reader["CLI_DIRECCION"].ToString().Trim();
                eCliente.CLI_TELEFONO = reader["CLI_TELEFONO"].ToString().Trim();
                eCliente.CLI_FAX = reader["CLI_FAX"].ToString().Trim();
                eCliente.CLI_CELULAR = reader["CLI_CELULAR"].ToString().Trim();
                eCliente.CLI_EMAIL = reader["CLI_EMAIL"].ToString().Trim();
                if (reader["CLI_FCH_NACIM"]!=DBNull.Value)
                {
                    eCliente.CLI_FCH_NACIM = Convert.ToDateTime(reader["CLI_FCH_NACIM"]);
                };

                EGrupo eGrupo = new EGrupo();
                eGrupo = nGrupo.GetGrupo(eCliente.GRU_CODIGO);
                eCliente.GRUPO = eGrupo;

                lista.Add(eCliente);

            }
            reader.Close();
            return lista;
        }
        catch
        {
            throw;
        }
        finally
        {
            conexion.Close();
        }

    }

As you can see it brings me a list of entities.

Now how do I get back a class that returns an entity? Ejm:

    public ECliente GetCliente(int Codigo) {...}

without having to reprogram the SQL and filling the datareader to the class to optimize the code.

Thanks for your help.

    
asked by Guivan 16.08.2017 в 22:09
source

1 answer

0

You have two ways of doing it.

One is controlling where you call your GetListClients. This only if you are sure that you will always return 1 record.

public ECliente CallCliente(string cedula)
{
   return GetListClientes(cedula)[0];
}

public List<ECliente> CallClientes()
{
  return GetListClientes(string.Empty);
}

In this way, you reuse code and handle it from a single method.

The other possibility is necessarily to create another method that only returns an ECient and not a List. I advise you the first option.

Finally, I gave myself the freedom to make some small changes to your code

public List<ECliente> GetListClientes(string criterio)
    {
        List<ECliente> lista = new List<ECliente>();
        string consulta = "SELECT CLI.CLI_CODIGO, CLI.CLI_NOMBRE... FROM ... WHERE 1=1 "
        /*Suponiendo que tienes un objeto tipo SqlCommand que se llame command*/
        if (!String.IsNullOrEmpty(criterio))
        {
            consulta += " AND CAMPO_BUSQUEDA = @CRITERIO";
            command.Parameters.AddWithValue(@CRITERIO, criterio);
        }
        try
        {
            conexion.Open();
            DataTable dt = new DataTable();
            dt.Load(command.ExecuteReader());    
            foreach (DataRow a in dt.Rows)
            {    
                ECliente eCliente = new ECliente();
                eCliente.CLI_CODIGO = (Int32)a["CLI_CODIGO"];
                eCliente.CLI_NOMBRE = a["CLI_NOMBRE"].ToString().Trim();
                eCliente.CLI_RUC = a["CLI_RUC"].ToString().Trim();
                eCliente.CLI_DIRECCION = a["CLI_DIRECCION"].ToString().Trim();
                eCliente.CLI_TELEFONO = a["CLI_TELEFONO"].ToString().Trim();
                eCliente.CLI_FAX = a["CLI_FAX"].ToString().Trim();
                eCliente.CLI_CELULAR = a["CLI_CELULAR"].ToString().Trim();
                eCliente.CLI_EMAIL = a["CLI_EMAIL"].ToString().Trim();
                if (reader["CLI_FCH_NACIM"]!=DBNull.Value)
                {
                    eCliente.CLI_FCH_NACIM = Convert.ToDateTime(a["CLI_FCH_NACIM"]);
                };

                EGrupo eGrupo = new EGrupo();
                eGrupo = nGrupo.GetGrupo(eCliente.GRU_CODIGO);
                eCliente.GRUPO = eGrupo;    
                lista.Add(eCliente);    
            }
            return lista;
        }
        catch
        {
            throw;
        }
        finally
        {
            conexion.Close();
        }    
    }

I hope you serve, greetings!

    
answered by 16.08.2017 / 22:28
source