7
I have a project that I am developing in layers, so far I have the following: Access to data, presentation, business and transfer objects. The doubt is as follows:
Example: I have one form
to register a legal entity in the database. To save the data that the user entered in form
would be correct in this form
that the user entered the data I create an object pessoaJuridica
and pass this object to a class in the business layer named SalvaPessoaJuridica.cs
for example, this in turn accesses the data access layer and writes to the database?
For each form
I would have to create a class to keep the data or I would already use the data access layer directly in the form and maintain the data without creating a specific class for each type of operation?
Example:
private void button1_Click(object sender, EventArgs e)
{
if (txtNome.Text == string.Empty || txtIdade.Text == string.Empty || txtEmail.Text == string.Empty)
{
MessageBox.Show("Informe os valores para nome, idade e email do aluno.");
return;
}
else
{
try
{
string sqlInsert = "Insert into Alunos (Nome,Idade,Email) values (@Nome,@Idade,@Email)";
string[] campos = { "@Nome", "@Idade", "@Email" };
string[] valores = { txtNome.Text, txtIdade.Text, txtEmail.Text };
accDB.Salvar(campos, valores, sqlInsert);
gdvDados.DataSource = accDB.getRegistro(sqlSelect);
}
catch (Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
}
Or:
private void button1_Click(object sender, EventArgs e)
{
try
{
Alunos aluno = new Alunos();
aluno.Nome = "Teste";
aluno.Idade = 22;
aluno.Email = "[email protected]"
SalvaAluno salva = new SalvaAluno();
salva.Manter(aluno);
}
catch (Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
}
The second option makes system maintenance easy. Any new member of your team will be able to easily understand what is done in this code. Note: Be careful when displaying exception messages to the end user and be very careful especially when concatenating strings to mount a query. You can open a security breach in your system.
– Lutti Coelho
Relocated. This question and the related questions can give a better view than do
– Bruno Costa