Methods with Dynamic C#parameter

Asked

Viewed 325 times

4

I need your help with the following question.

I have a data access class that has a method that takes the parameters to run a stored database.

The problem is this, I have several objects in the application and I am trying to create a class that receives one of these objects and execute the method of adding parameters according to the received object.

Follow code done so far:

Method of the data access class receiving the parameters.

    public void AddParamentros(string parametro, dynamic valorParametro)
    {
        listaParamentrosSql.Add(new SqlParameter { ParameterName = parametro, Value = valorParametro });
    }

Use of the data class:

public class ManterDados
{
    AcessoDadosSql acessoDados = new AcessoDadosSql();

    public string manter(dynamic objeto, StoredProcedures procedure)
    {
        //Limpa parâmetros adicionados
        acessoDados.LimparParametros();
        // Adiciona os parametros 
        addParametrosSql addParam = new addParametrosSql(objeto);
        // Executa a SP
        string retorno = acessoDados.ExecutaSql(System.Data.CommandType.StoredProcedure, procedure);
        return retorno;
    }

}

This is the class I’m in trouble with:

public class addParametrosSql
{


    AcessoDadosSql acessoDados = new AcessoDadosSql();

    private void adicionarParametros(PessoaJuridica pessoaJuridica)
    {

        acessoDados.AddParamentros("@Cnpj", pessoaJuridica.CNPJ);
        acessoDados.AddParamentros("@IE", pessoaJuridica.INSCRICAO_ESTADUAL);
        acessoDados.AddParamentros("@Suframa", pessoaJuridica.SUFRAMA);

    }
    private void adicionarParametros(PessoaFisica pessoaFisica)
    {

        acessoDados.AddParamentros("@Cpf", pessoaFisica.CPF);
        acessoDados.AddParamentros("@Nome", pessoaFisica.NOME);

    }
}

What is the correct procedure for the addParametrosSql class to execute the add parameter method according to the object passed as parameter?

When I pass the parameter as Dynamic, even being of the Personal type or personal.

I tried to create a constructor to convert the received parameter to the correct type, but it didn’t work.

EDIT:

This way is working, would be able to improve this code?

    private void adicionarParamentros(object objeto)
    {
        acessoDados.LimparParametros();

        Type tipo = objeto.GetType();

        foreach (PropertyInfo propriedade in tipo.GetRuntimeProperties())
        {
             acessoDados.AddParamentros("@" + propriedade.Name, propriedade.GetValue(objeto, null));
        }
    }

1 answer

4


Honestly, this way will generate you more rework than help you.

First, I’d leave your class addParametrosSql generic. By the way, the name is horrible. I will choose a better name, ok?

public class ParametrosSqlHelper<TClasse>
    where TClasse: class, new()
{
    AcessoDadosSql acessoDados = new AcessoDadosSql();
    private TClasse _objeto;

    private ParametrosSqlHelper() { } // Evita de ter construtor público vazio, se você for fazer com construtor mesmo.
    public ParametrosSqlHelper(TClasse objeto) // Acho que não precisa.
    {
        _objeto = objeto;
    }

    private void adicionarParametros(TClasse classe)
    {
        foreach (var propriedade in classe.GetType().GetProperties()) 
        {
            acessoDados.AddParamentros("@" + propriedade.Name, propriedade.GetValue(classe, null));
        }
    }
}

ManterDados would look like this:

public class ManterDados
{
    AcessoDadosSql acessoDados = new AcessoDadosSql();

    public string manter(object objeto, StoredProcedures procedure)
    {
        //Limpa parâmetros adicionados
        acessoDados.LimparParametros();
        // Adiciona os parametros 
        var addParam = new ParametrosSqlHelper<PessoaJuridica>(objeto);
        // Executa a SP
        string retorno = acessoDados.ExecutaSql(System.Data.CommandType.StoredProcedure, procedure); // Eu não entendi isso aqui.
        return retorno;
    }
}

AddParametros is with the wrong spelling, and I do not know if it needs to exist even because there is no way to define the typing of the parameter, but outside the spelling is ok too.

public void AddParametros(string parametro, dynamic valorParametro)
{
    listaParamentrosSql.Add(new SqlParameter { ParameterName = parametro, Value = valorParametro });
}
  • Gypsy thanks for the help, in this part "var addparam = new Parametrossqlhelper<Personal>(object);" you are passing the Personal type, my idea was to create a class that I passed the campus without type and the class that would be responsible for "knowing" what kind that was and execute the method, it would be feasible this?

  • You can do it in tipless as well. My approach using generic class (TClasse) is just a suggestion.

  • 1

    I get it, I’m gonna do it on the down low. I will have to make a change to the DTO of the project because I was not leaving the application classes according to the database objects, so in the "foreach" gave parameter error. Thank you so much for your help.

  • 1

    +1, very good, I’ll even try to join

Browser other questions tagged

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