layered development c # (BBL, DAL, Model, GUI) [closed]

1

In my project I'm developing in C # using Form Form windows with the default Layer development I'm not sure if this form is correct? because I already researched a lot on layers, but I realize that each person develops in a way.

Have the following layers:

  • BBL - business rules or validations;

  • DAL - for bank access

  • GUI - for forms and part of user interaction

  • Template - where the class templates for inheritance are.

To write a user I'm 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 - DAILY

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);

            }

        }
    }
}

Template - User template

  

just get and set and constructor ...

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

and in the UserGUI class should I only instantiate the UserBBL to be more secure and run the ValidateInserca?

These are my doubts that prevent me from developing in C #.

Thank you in advance for your attention.

    
asked by anonymous 11.03.2017 / 22:11

1 answer

1

You can not follow suit.

I've been programming for 29 years and a few years ago, with the desktop / web merge, I realized the advantage of dividing the layers:

  • DATA - No comments. It's like you already do. (clsDates ...)
  • VIEW (view_web / view_desktop / view_mobile) - This is where the team separates the presentation layer. (Windows Forms)
  • MODEL - The other classes (clsCompany, clsNotaFiscal ...)
  • CONTROL - Validations, Formatting functions, etc. (clsFuncoes, ...)

Think of a programming model UNLOCKED, where there is no dependency between layers.

Example 1: Avoid being in the CONTROL layer and 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 can not be in form1 and call:

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

It's how I do it and helps 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!

    
12.03.2017 / 15:08