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));
}
}
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?
– Robss70
You can do it in tipless as well. My approach using generic class (
TClasse
) is just a suggestion.– Leonel Sanches da Silva
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.
– Robss70
+1, very good, I’ll even try to join
– Artur Trapp