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.
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.
– Maniero
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.
– gato
Here has more information of the development based on layers.
– gato