Structure/Organization Project C#

Asked

Viewed 764 times

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.

  • Relocated. This question and the related questions can give a better view than do

1 answer

4


His second example is the one that came closest to best practice, as he is applying the concept of separation of responsibilities.

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

In the first example the presentation layer is being responsible for input logic in the database, such as creating the SQL statement and saving. The form’s responsibility is to collect and display data.

This class of yours SalvaAluno would be a kind of repository if using the Repository Pattern, because it just adds student-type entities.

For your current code to look even better instead of using the class SalvaAluno directly, you could use an interface ISalvaAlunoto apply concepts of dependency injection and control inversion. That way you wouldn’t worry about the kind of implementation of your DAL layer, this implementation could be either with Entity Framework, Dapper etc

For all this cited there is a lot of content available on this site and on the web.

Answering your question:

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 already would keep the data without creating a specific class for each type of operation?

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

Browser other questions tagged

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