c# layer development (BBL, DAL, Model, GUI)

Asked

Viewed 1,211 times

1

In my project I am developing in C# using windows Form Application with the pattern of development in Layers I am in doubt if this form is correct? because I’ve researched a lot about layers, but I realize that each person develops in a way.

Have the following layers:

  • BBL - business rules or validations;

  • DAL - for access to the bank

  • GUI - for forms and part user interaction

  • Model - where class models for inheritance are located.

To record a user I am developing this way:

DAL - Connection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Data;
using Npgsql;

namespace DAL
{
    public class Conexao
    {
        protected NpgsqlConnection Con; // estabele a conexao
        protected NpgsqlCommand Cmd; // executar e escrever os comandos sql
        protected NpgsqlDataReader Dr; //retorna os registro das querys das consultar
        // Dados da conexao 
        private string servidor = "127.0.0.1";   
        private string porta = "5432";
        private string userBD = "postgres";
        private string senhaBD = "123";
        private string banco = "teste";
        private string connString = null;

        protected void abrirConexao()
        {

            try
            {
                connString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                                servidor, porta, userBD, senhaBD, banco);
                Con = new NpgsqlConnection(connString);
                Con.Open();

            }
            catch (NpgsqlException ex)
            {
                Console.WriteLine("erro ao abrir conexao" + ex.Message);
                throw new NpgsqlException(ex.Message);

            }
        }

        protected void fecharConexao()
        {
            try
            {
                Con.Close();
            }
            catch (NpgsqlException ex)
            {

                throw new NpgsqlException(ex.Message);
            }
        }        
    }
}

DAL - User

namespace DAL
{
    public class UsuarioDAL : Conexao
    {
        public void insere(UsuarioModel usuario) 
        {
            try
            {
                //Abre a conexao para insercao
                abrirConexao();
                Cmd = new NpgsqlCommand("insert into \"Cadastro\".\"Usuario\" (id) values (@id)", Con);
                Cmd.Parameters.AddWithValue("@id", usuario.id);
                Cmd.ExecuteNonQuery();
                MessageBox.Show("Usuario Inserido com Sucesso");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao inserer usuario - Erro: " + ex.Message);

            }

        }
    }
}

Model - User model

only get and set and builder ...

Now in the package BBL - User should I make the validations ? create the Validate method and validate with Data Annotations and call the insert of DAL?

and in the User class I should only instantiate the User to be safer and run Validainsert?

Would those my doubts prevent me from developing in c#.

Thank you for your attention.

  • 1

    And my opinion is that almost everyone does it, but since it’s just opinion, for me to show the whole problem that they do it would take a long time, it would almost be a book, and that what I think is a little controversial, after all everyone does it differently, nor will I try to answer. Most likely someone will give an opinion and you will have to decide whether to follow or not.

  • In GUI you will need to popular your model, I can not say if it is the best way, but if I were in your place I would read more about layered development.

  • Here has more information of the development based on layers.

1 answer

1


You can’t follow through.

I’ve been programming for 29 years and a few years ago, with desktop/web merging, I realized the advantage of breaking down the layers:

  • DICE - No comments. It’s like you already do. (clsDados...)
  • VIEW (view_web/view_desktop/view_mobile) - This is where the team separates the presentation layer. (Windows Forms)
  • MODEL - Other classes (clsEmpresa, clsNotaFiscal...)
  • CONTROL - Validations, Formatting functions, etc. (clsFuncoes, ...)

Think of a DECOUPLED programming model where there is no dependency between one layer and the other.

Examples 1: Avoid being in the CONTROL layer and from there refer to objects in the VIEW layer:

public static void fRetiraAcentuacao(string texto)
{ 
   form2.txtCNPJ.Text = texto.Replace()... ; 
}

I find it more elegant and decoupled like this:

public static void fRetiraAcentuacao(string texto, ref TextBox txt)
{ 
   txt.Text = texto.Replace()... ; 
}

Example2: I cannot be in Form1 and call:

**form2**.TextBox.Text = string.Empty;

It’s how I do and help with programming within development teams.

Another thing: always remember that another programmer needs to maintain your code. Comment on what is not obvious.

Eager to see how others do!

  • Thank you for the reply Diego. So I must create a model User class with the get and set Constutor... After creating a class Usuariocontrol that will do for example the data validations, for example Insert, it will validate the information and call the class User? And use Inserts inside the view? to get better?

  • That’s it! This book is old, but it deals with development in three layers: https://goo.gl/7jL4xx

Browser other questions tagged

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