1-) C# - Bölüm 22 OOP ile N Katmanlı Mimaride Proje Geliştirme
Katmanlar
Entity Layer= veri tabanındaki tabloları ve stunları oluşturucağımız katman.
Data Access Layer=veri erişim katmanı.
Logic Layer=Mantık Katmanı.
Presentation Layer=Sunum katmanı.(UI)
Normal bir windows form oluşturdukdan sonra solution a sağ tıklayıp add diyip yeni bir proje oluşturucaz bu proje Class Library(.NET Framework).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityLayer
{
public class EntityPersonel
{
private int id;
private string ad;
private string soyad;
private string sehir;
private string gorev;
private short maas;
public int Id { get => id; set => id = value; }
public string Ad { get => ad; set => ad = value; }
public string Soyad { get => soyad; set => soyad = value; }
public string Sehir { get => sehir; set => sehir = value; }
public string Gorev { get => gorev; set => gorev = value; }
public short Maas { get => maas; set => maas = value; }
}
}
Not CTRL R E İLE TANINMIŞ OLAN DEĞİŞKENLERİ PROPÖRTİ YAPIYORUZ =).
sql bağlantısı alma; project-add new data source-database-next-dataset-next-new connection- servername ye sql bağlantısının giriş ıd sini kopyala yapıştır yapıyoruz,daha sonra select or enter dan databasemizi seçiyoruz.sonrada show the connectiondan bağlantımızı alıyoruz.
public static SqlConnection bgl = new SqlConnection(@"Data Source = DESKTOP - 9BC05PL; Initial Catalog = DbPersonel; Integrated Security = True");
References ekleme,references sağ tık- add reference-project-EntityLayer ok.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
public class DALPersonel
{
public static List<EntityPersonel> PersonelListesi() // geriye döndüren METOT
{
List<EntityPersonel> degerler = new List<EntityPersonel>();
SqlCommand komut1 = new SqlCommand("Select * from TBLBILGI", Baglanti.bgl);
if (komut1.Connection.State != ConnectionState.Open)
{
komut1.Connection.Open();
}
SqlDataReader dr = komut1.ExecuteReader();
while (dr.Read())
{
EntityPersonel ent = new EntityPersonel();
ent.Id = int.Parse(dr["ID"].ToString());
ent.Ad = dr["AD"].ToString();
ent.Soyad = dr["SOYAD"].ToString();
ent.Gorev = dr["GOREV"].ToString();
ent.Maas = short.Parse(dr["MAAS"].ToString());
degerler.Add(ent);
}
dr.Close();
return degerler;//döndürüldü.
}
}
}
Logic katmanı
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data;
using DataAccessLayer;
namespace LogicLayer
{
public class LogicPersonel
{
public static List<EntityPersonel> LLPersonelListesi()
{
return DALPersonel.PersonelListesi();
}
}
}
Son katman
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EntityLayer;
using DataAccessLayer;
using LogicLayer;
namespace NKatmanliMimari
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnListele_Click(object sender, EventArgs e)
{
List<EntityPersonel> PerList = LogicPersonel.LLPersonelListesi();
dataGridView1.DataSource = PerList;
}
}
}
Personel ekleme
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data;
using DataAccessLayer;
namespace LogicLayer
{
public class LogicPersonel
{
public static List<EntityPersonel> LLPersonelListesi()
{
return DALPersonel.PersonelListesi();
}
public static int LLPersonelEkle(EntityPersonel p)
{
if (p.Ad != "" && p.Soyad != "" && p.Maas >= 3500 && p.Ad.Length >= 3)
{
return DALPersonel.PersonelEkle(p);
}
else
{
return -1;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
public class DALPersonel
{
public static List<EntityPersonel> PersonelListesi()
{
List<EntityPersonel> degerler = new List<EntityPersonel>();
SqlCommand komut1 = new SqlCommand("Select * from TBLBILGI", Baglanti.bgl);
if (komut1.Connection.State != ConnectionState.Open)
{
komut1.Connection.Open();
}
SqlDataReader dr = komut1.ExecuteReader();
while (dr.Read())
{
EntityPersonel ent = new EntityPersonel();
ent.Id = int.Parse(dr["ID"].ToString());
ent.Ad = dr["AD"].ToString();
ent.Soyad = dr["SOYAD"].ToString();
ent.Gorev = dr["GOREV"].ToString();
ent.Sehir = dr["SEHIR"].ToString();
ent.Maas = short.Parse(dr["MAAS"].ToString());
degerler.Add(ent);
}
dr.Close();
return degerler;
}
public static int PersonelEkle(EntityPersonel p)
{
SqlCommand komut2 = new SqlCommand("insert into TBLBILGI (AD,SOYAD,GOREV,SEHIR,MAAS) VALUES (@P1,@P2,@P3,@P4,@P5)", Baglanti.bgl);
if (komut2.Connection.State != ConnectionState.Open)
{
komut2.Connection.Open();
}
komut2.Parameters.AddWithValue("@P1", p.Ad);
komut2.Parameters.AddWithValue("@P2", p.Soyad);
komut2.Parameters.AddWithValue("@P3", p.Sehir);
komut2.Parameters.AddWithValue("@P4", p.Gorev);
komut2.Parameters.AddWithValue("@P5", p.Maas);
return komut2.ExecuteNonQuery();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EntityLayer;
using DataAccessLayer;
using LogicLayer;
namespace NKatmanliMimari
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnListele_Click(object sender, EventArgs e)
{
List<EntityPersonel> PerList = LogicPersonel.LLPersonelListesi();
dataGridView1.DataSource = PerList;
}
private void BtnEkle_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Ad = TxtAd.Text;
ent.Soyad = TxtSoyad.Text;
ent.Sehir = TxtSehir.Text;
ent.Maas = short.Parse(TxtMaas.Text);
ent.Gorev = TxtGorev.Text;
LogicPersonel.LLPersonelEkle(ent);
}
}
}
Silme işlemi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data;
using DataAccessLayer;
namespace LogicLayer
{
public class LogicPersonel
{
public static List<EntityPersonel> LLPersonelListesi()
{
return DALPersonel.PersonelListesi();
}
public static int LLPersonelEkle(EntityPersonel p)
{
if (p.Ad != "" && p.Soyad != "" && p.Maas >= 3500 && p.Ad.Length >= 3)
{
return DALPersonel.PersonelEkle(p);
}
else
{
return -1;
}
}
public static bool LLPersonelSil(int per)
{
if (per >= 1)
{
return DALPersonel.PersonelSil(per);
}
else
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
public class DALPersonel
{
public static List<EntityPersonel> PersonelListesi()
{
List<EntityPersonel> degerler = new List<EntityPersonel>();
SqlCommand komut1 = new SqlCommand("Select * from TBLBILGI", Baglanti.bgl);
if (komut1.Connection.State != ConnectionState.Open)
{
komut1.Connection.Open();
}
SqlDataReader dr = komut1.ExecuteReader();
while (dr.Read())
{
EntityPersonel ent = new EntityPersonel();
ent.Id = int.Parse(dr["ID"].ToString());
ent.Ad = dr["AD"].ToString();
ent.Soyad = dr["SOYAD"].ToString();
ent.Gorev = dr["GOREV"].ToString();
ent.Sehir = dr["SEHIR"].ToString();
ent.Maas = short.Parse(dr["MAAS"].ToString());
degerler.Add(ent);
}
dr.Close();
return degerler;
}
public static int PersonelEkle(EntityPersonel p)
{
SqlCommand komut2 = new SqlCommand("insert into TBLBILGI (AD,SOYAD,GOREV,SEHIR,MAAS) VALUES (@P1,@P2,@P3,@P4,@P5)", Baglanti.bgl);
if (komut2.Connection.State != ConnectionState.Open)
{
komut2.Connection.Open();
}
komut2.Parameters.AddWithValue("@P1", p.Ad);
komut2.Parameters.AddWithValue("@P2", p.Soyad);
komut2.Parameters.AddWithValue("@P3", p.Sehir);
komut2.Parameters.AddWithValue("@P4", p.Gorev);
komut2.Parameters.AddWithValue("@P5", p.Maas);
return komut2.ExecuteNonQuery();
}
public static bool PersonelSil(int p)
{
SqlCommand komut3 = new SqlCommand("Delete from TBLBILGI where ID=@P1", Baglanti.bgl);
if (komut3.Connection.State != ConnectionState.Open)
{
komut3.Connection.Open();
}
komut3.Parameters.AddWithValue("@P1", p);
return komut3.ExecuteNonQuery() > 0;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EntityLayer;
using DataAccessLayer;
using LogicLayer;
namespace NKatmanliMimari
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnListele_Click(object sender, EventArgs e)
{
List<EntityPersonel> PerList = LogicPersonel.LLPersonelListesi();
dataGridView1.DataSource = PerList;
}
private void BtnEkle_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Ad = TxtAd.Text;
ent.Soyad = TxtSoyad.Text;
ent.Sehir = TxtSehir.Text;
ent.Maas = short.Parse(TxtMaas.Text);
ent.Gorev = TxtGorev.Text;
LogicPersonel.LLPersonelEkle(ent);
}
private void BtnSil_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Id = Convert.ToInt32(textBox1.Text);
LogicPersonel.LLPersonelSil(ent.Id);
}
}
}
Güncelleme işlemi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data;
using DataAccessLayer;
namespace LogicLayer
{
public class LogicPersonel
{
public static List<EntityPersonel> LLPersonelListesi()
{
return DALPersonel.PersonelListesi();
}
public static int LLPersonelEkle(EntityPersonel p)
{
if (p.Ad != "" && p.Soyad != "" && p.Maas >= 3500 && p.Ad.Length >= 3)
{
return DALPersonel.PersonelEkle(p);
}
else
{
return -1;
}
}
public static bool LLPersonelSil(int per)
{
if (per >= 1)
{
return DALPersonel.PersonelSil(per);
}
else
{
return false;
}
}
public static bool LLPersonelGuncelle(EntityPersonel ent)
{
if (ent.Ad.Length >= 3 && ent.Ad != "" && ent.Maas >= 4500)
{
return DALPersonel.PersonelGuncelle(ent);
}
else
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
public class DALPersonel
{
public static List<EntityPersonel> PersonelListesi()
{
List<EntityPersonel> degerler = new List<EntityPersonel>();
SqlCommand komut1 = new SqlCommand("Select * from TBLBILGI", Baglanti.bgl);
if (komut1.Connection.State != ConnectionState.Open)
{
komut1.Connection.Open();
}
SqlDataReader dr = komut1.ExecuteReader();
while (dr.Read())
{
EntityPersonel ent = new EntityPersonel();
ent.Id = int.Parse(dr["ID"].ToString());
ent.Ad = dr["AD"].ToString();
ent.Soyad = dr["SOYAD"].ToString();
ent.Gorev = dr["GOREV"].ToString();
ent.Sehir = dr["SEHIR"].ToString();
ent.Maas = short.Parse(dr["MAAS"].ToString());
degerler.Add(ent);
}
dr.Close();
return degerler;
}
public static int PersonelEkle(EntityPersonel p)
{
SqlCommand komut2 = new SqlCommand("insert into TBLBILGI (AD,SOYAD,GOREV,SEHIR,MAAS) VALUES (@P1,@P2,@P3,@P4,@P5)", Baglanti.bgl);
if (komut2.Connection.State != ConnectionState.Open)
{
komut2.Connection.Open();
}
komut2.Parameters.AddWithValue("@P1", p.Ad);
komut2.Parameters.AddWithValue("@P2", p.Soyad);
komut2.Parameters.AddWithValue("@P3", p.Sehir);
komut2.Parameters.AddWithValue("@P4", p.Gorev);
komut2.Parameters.AddWithValue("@P5", p.Maas);
return komut2.ExecuteNonQuery();
}
public static bool PersonelSil(int p)
{
SqlCommand komut3 = new SqlCommand("Delete from TBLBILGI where ID=@P1", Baglanti.bgl);
if (komut3.Connection.State != ConnectionState.Open)
{
komut3.Connection.Open();
}
komut3.Parameters.AddWithValue("@P1", p);
return komut3.ExecuteNonQuery() > 0;
}
public static bool PersonelGuncelle(EntityPersonel ent)
{
SqlCommand komut4 = new SqlCommand("Update TBLBILGI SET AD=@P1,SOYAD=@P2,MAAS=@P3,SEHIR=@P4,GOREV=@P5 WHERE ID=@P6", Baglanti.bgl);
if (komut4.Connection.State != ConnectionState.Open)
{
komut4.Connection.Open();
}
komut4.Parameters.AddWithValue("@P1", ent.Ad);
komut4.Parameters.AddWithValue("@P2", ent.Soyad);
komut4.Parameters.AddWithValue("@P3", ent.Maas);
komut4.Parameters.AddWithValue("@P4", ent.Sehir);
komut4.Parameters.AddWithValue("@P5", ent.Gorev);
komut4.Parameters.AddWithValue("@P6", ent.Id);
return komut4.ExecuteNonQuery() > 0;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EntityLayer;
using DataAccessLayer;
using LogicLayer;
namespace NKatmanliMimari
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnListele_Click(object sender, EventArgs e)
{
List<EntityPersonel> PerList = LogicPersonel.LLPersonelListesi();
dataGridView1.DataSource = PerList;
}
private void BtnEkle_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Ad = TxtAd.Text;
ent.Soyad = TxtSoyad.Text;
ent.Sehir = TxtSehir.Text;
ent.Maas = short.Parse(TxtMaas.Text);
ent.Gorev = TxtGorev.Text;
LogicPersonel.LLPersonelEkle(ent);
}
private void BtnSil_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Id = Convert.ToInt32(textBox1.Text);
LogicPersonel.LLPersonelSil(ent.Id);
}
private void BtnGuncelle_Click(object sender, EventArgs e)
{
EntityPersonel ent = new EntityPersonel();
ent.Id = Convert.ToInt32(textBox1.Text);
ent.Ad = TxtAd.Text;
ent.Soyad = TxtSoyad.Text;
ent.Sehir = TxtSehir.Text;
ent.Gorev = TxtGorev.Text;
ent.Maas = short.Parse(TxtMaas.Text);
LogicPersonel.LLPersonelGuncelle(ent);
}
}
}