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.