Structure / Organization Project C #

7

I have a project that I'm developing in layers, so far I have the following: Access to data, presentation, business and transfer objects. The question is:

Example: I have 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 a pessoaJuridica object and pass this object to a class in the business layer with the name SalvaPessoaJuridica.cs by example, does this in turn access the data access layer and write to the bank?

For every form would I have to create a class to hold the data or would I already use the data access layer directly on the form and would I already keep the data without creating a class specific to 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);
            }
        }
     }
    
asked by anonymous 26.10.2016 / 15:16

1 answer

4

Your second example is the one that came closest to best practices, as it is applying the concept of separation of responsibilities .

This second code is something very similar to that the concepts MVC or MVVM would do for example.

In the first example the presentation layer is being responsible for insertion logic in the database, how to create the SQL statement and save. The form's responsibility is to collect and display data.

This your SalvaAluno class would be a kind of repository if you were using the Repository Pattern because it only adds student type entities.

For your current code to look better instead of using the SalvaAluno class directly, you could use a ISalvaAluno interface to apply concepts from # In this way you would not worry about the type of implementation of your DAL layer, this implementation could be both with Entity Framework, Dapper etc

For all of that quoted there is a lot of content available on this website and on the web.

Answering your question:

  

For each form I would have to create a class to hold the data or   I would already use the data access layer directly on the form and already   would maintain the data without creating a class specific to each type of   operation?

You would have to create a class to persist data for each form (or better for each entity) for separation of responsibilities.

    
01.11.2016 / 20:14