.Net Core Web API — Dapper ORM Kullanımı — Katmanlı Mimari

Kağan Saygın
4 min readJun 11, 2021

--

Önceki “Dapper Nedir? Nasıl Kullanılır ?” yazımda Dapper’dan bahsetmiştim. Bu yazımda da katmanlı mimari ve Dapper kullanımını inceleyeceğiz.

Katmanlı Mimari ile Dapper ORM Kullanımı

Projemizde efsane veritabanı olan Northwind veritabanı ile ürün seçme, listeleme, ekleme, güncelleme, silme gibi işlemler yapacağız.

Projemizin yapısından biraz bahsedeyim.

Bussiness: DataAccess katmanından gelen verilerimizi işleyecek katmandır.

  • Abstract: Servislerimizin bulunduğu klasör.
  • IProductService: Product işlemleri için dışarıdan bu servis ile erişip işlemleri uygulayacağımız arayüzdür.
  • Concrete: Manager sınıflarımızın bulunduğu klasör.
  • ProductManager: Product işlemlerini yapacağımız sınıf.

DataAccess: Veritabanı erişim katmanımız.

  • Abstract: Interfacelerin bulunduğu klasör
  • IRepository: Bu interface sayesinde sürekli GetAll,GetById,Add,Remove gibi metotları her bir interface için yazmamız gerekmeyecek.
  • → IProductDal: IRepository interfacenin implemente ederiz.
Dapper Orm
Dapper MİCRO ORM
  • Concrete
  • Dapper
  • → DapperRepositoryBase: ConnectionString tanımlıyoruz. (appsettings.json dosyasının içinde “SqlConnection” adında)
  • → DpProductDal: Veritabanı ile yapacağımız işlemler burada olacak Dapper’ı burada kullanacağız o yüzden burayı biraz daha detaylandıracağım.
//Sql Connection için DapperRepositoryBase,
//Metotlarımız için IProductDal implemente ediyoruz.
DpProductDal : DapperRepositoryBase, IProductDal
  • List<Product> GetAll() Metodu

connection.Query<Entity>(“SQL SORGUSU”)

Sorgu çalıştıktan sonra tanımladığınız entity şeklinde geri döner.

Product tanımladık, gelen productları listeye çevirdik.

public List<Product> GetAll()
{
using var connection = new SqlConnection(_connectionString);
return connection.Query<Product>("SELECT * FROM Products").ToList();
}
  • Product GetById(int productId) Metodu

Kullanıma geçmeden önce önemli bir hatırlatma yapmak isterim. Dapper kullanırken sql sorgularının saf halini yazıyoruz bu da bize SQL INJECTION zafiyetini oluşturacaktır, sorgularımızı parametreli olarak yazmalıyız.

connection.QueryFirst<Entity>(sqlSorgusu, new { TabloKolon= parametre});

.Query() yerine .QueryFirst() kullandık böylelikle eşleşen ilk satırı geri döndürecektir.

using var connection = new SqlConnection(_connectionString);string sql = "SELECT * FROM Products WHERE ProductID = @ProductID";return connection.QueryFirst<Product>(sql, new { ProductID = productId });
  • int Add(Product product),
  • int Remove(int productId),
  • int Update(Product product) Metotları

Ekleme, silme, güncelleme işlemi yapacağımız metotlardır. Bu sefer .Query() ve .QueryFirst() yerine .Execute() kullanmamız gerekiyor geriye int tipinde değer döndürüyor. Bilgileri parametreli olarak eklemeliyiz.

using var connection = new SqlConnection(_connectionString);string sql = "INSERT INTO Products (ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued) VALUES (@ProductName,@SupplierID,@CategoryID,@QuantityPerUnit,@UnitPrice,@UnitsInStock,@UnitsOnOrder,@ReorderLevel,@Discontinued);return connection.Execute(sql, new
{
ProductName = product.ProductName,
SupplierID = product.SupplierId,
CategoryID = product.CategoryId,
QuantityPerUnit = product.QuantityPerUnit,
UnitPrice = product.UnitPrice,
UnitsInStock = product.UnitsInStock,
UnitsOnOrder = product.UnitsOnOrder,
ReorderLevel = product.ReorderLevel,
Discontinued = product.Discontinued
});
public int Remove(int productId)
{
using var connection = new SqlConnection(_connectionString);
string sql = "DELETE FROM Products WHERE ProductID = @productID;";return connection.Execute(sql, new { ProductID = productId });
}
public int Update(Product product)
{
using var connection = new SqlConnection(_connectionString);
string sql = "UPDATE Products SET ProductName=@ProductName,SupplierID=@SupplierID,CategoryID=@CategoryID,QuantityPerUnit=@QuantityPerUnit,UnitPrice=@UnitPrice,UnitsInStock=@UnitsInStock,UnitsOnOrder=@UnitsOnOrder,ReorderLevel=@ReorderLevel,Discontinued=@Discontinued WHERE ProductId=@ProductId";return connection.Execute(sql, new
{
ProductId = product.ProductId,
ProductName = product.ProductName,
SupplierID = product.SupplierId,
CategoryID = product.CategoryId,
QuantityPerUnit = product.QuantityPerUnit,
UnitPrice = product.UnitPrice,
UnitsInStock = product.UnitsInStock,
UnitsOnOrder = product.UnitsOnOrder,
ReorderLevel = product.ReorderLevel,
Discontinued = product.Discontinued
});
}

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Entites: Veritabanındaki tablolara karşılık gelen model sınıflarımızın olduğu katman.

  • Product: Northwind veritabanındaki Product tablomuzun karşılığındaki modelimiz.

WebAPI: Kullanıcı ile etkileşime geçeceğimiz sunum katmanı.

→ ProductsController: Product bilgileri için kullanıcıdan alacağımız veya göndereceğimiz controller.

→ Startup.cs: Bu yapıya göre projeyi başlattığınızda hata alacaksınız. Startup sınıfında servislerimizi dahil etmeliyiz.

services.AddSingleton<IProductService, ProductManager>();
services.AddSingleton<IProductDal, DpProductDal>();
  • services.AddSingleton: Her zaman aynı nesneyi kullanır.
  • services.AddScoped: Nesneyi her request’de bir adet üretir ve her yerde kullanır.
  • services.AddTransient: Her istekte yeni bir nesne üretir.

--

--

No responses yet