Dapper - bu SQL so'rovlari natijalarini C# sinflari bilan taqqoslaydigan almashinuv vositasi. Ishlash tizimi EntityFreamwork ga ham o’xshaydi,ammo undan tezroq ishlaydi. Hammamiz biladigan https://stackoverflow.com/ saytining qidiruv tizimi ham Dapper yordamida tuzilgan.
Shuningdek Dapper oldingi Ado.Net ning avlodi hisoblanadi.Shuning uchun ham tuzilma Ado.Net bilan juda o'xshash.
Endi amaliyot bilan shug’ullansak, Man Dapper bilan ishlash ko’nikmasini shakllantirish uchun,kompyuterni uncha qiynamaydigan bir nechta dasturlarni o’rnatib olaman.VsCode, MsSqlLocalDb, Dotnet lar bo’lsa bo’ldi.
Yangi jadval quramiz. (Bu kodlar faqat MsSql uchun)
CREATETABLEPeople ( Id INTPRIMARY KEY, FullName VARCHAR(MAX), Age INTNOT NULL, Email VARCHAR(MAX) NOT NULL);
Jadvalni to’ldiramiz. (Bu kodlar faqat MsSql uchun)
INSERT INTO People VALUES (1,'Ruzimurod Abdunazarov',19,'ruzimurodabdunazarov2003@mail.ru');INSERT INTO People VALUES (2,'Eldor Axmedov',18,'eldor04@mail.ru');INSERT INTO People VALUES (3,'Umid Abdusattorov',35,'umid.abdusattorov@mail.ru');INSERT INTO People VALUES (4,NULL,11,'testor06@mail.ru');INSERT INTO People VALUES (5,'Farzona Holmo''minova',16,'farzona2007@mail.ru');INSERT INTO People VALUES (6,'Doston Ibragimov',30,'dostonI23@mail.ru');INSERT INTO People VALUES (7,'Jasur Tursunov',25,'jasur13@mail.ru');INSERT INTO People VALUES (8,NULL,24,'testor2@mail.ru');INSERT INTO People VALUES (9,'Mohichexra Davronova',16,'mohichexra2007@mail.ru');INSERT INTO People VALUES (10,'Davron Sattorov',26,'davron165@mail.ru');
Keyin umumiy malumotlar ombori quyidagicha bo’ladi:
Birinchi IUserRepository interfeysini qurib olamiz. Endi UserRepository ni yozamiz. Kodlar asosan SQL ni o’zidagi Query da yoziladi, shuning uchun ham Dapper EfCore ga qaraganda tezroq ishlaydi.
1] Umumiy barcha User larni olib kelish uchun quyidagicha qilamiz.
publicList<User> GetAll(){using (IDbConnection db =newSqlConnection(_connectionString)) {returndb.Query<User>("SELECT * FROM People").ToList(); }}
Shu yerda bir narsaga e’tibor bering: SELECT * FROM People umumiy malumotlarni olib keldi. Query() buyrug'i buni object ga aylantirayapti.
2] Aynan qaysidir Userni olib kelish uchun (SQL da SELECT buyruqi yordamida)
publicUserGet(int id){using (IDbConnection db =newSqlConnection(_connectionString)) {returndb.QuerySingleOrDefault<User>("SELECT * FROM People WHERE Id = @id",new { id }) ??newUser { }; }}
Bu yerda QuerySingleOrDefault() metodi bitta id parametrini Id ga tenglayapti.
3] Yangi User qo’shish uchun (SQL da INSERT buyruqi yordamida)
publicvoidCreate(User user){using (IDbConnection db =newSqlConnection(_connectionString)) {var sqlQuery ="INSERT INTO People VALUES(@Id, @FullName, @Age, @Email)";db.Execute(sqlQuery, user); }}
4] Malum bir Userni o’zgartirish uchun (SQL da UPDATE buyruqi yordamida)
publicvoidUpdate(User user){using (IDbConnection db =newSqlConnection(_connectionString)) {var sqlQuery ="UPDATE People SET FullName = @FullName, Age = @Age, Email = @Email WHERE Id = @Id";db.Execute(sqlQuery, user); }}
5] Malum bir Userni o’chirish uchun (SQL da DELETE buyruqi yordamida)
publicvoidDelete(int id){using (IDbConnection db =newSqlConnection(_connectionString)) {var sqlQuery ="DELETE FROM People WHERE Id = @id";db.Execute(sqlQuery,new { id }); }}
6] Ma’lum bir prodsedureni chaqirish uchun (SQL da EXEC buyruqi yordamida) Eng avvalo string DateTime ni Full qilib beradigan bitta prodsedura yaratib olaman:
using (IDbConnection db =newSqlConnection(_connectionString)){var sqlQuery ="EXEC ToFullDateTime @dateTime";FullTime result =db.QuerySingleOrDefault<FullTime>(sqlQuery,new { dateTime =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });return$"Год - {result.Year}, Четверть - {result.Quarter}, Месяц название - {result.MonthName}, День года - {result.DayOfYear}, День - {result.Day}, День недели - {result.DayOfTheWeek}, Час - {result.Hour}, Минута - {result.Minute}, Секунд - {result.Second}"; }
b] CommandType.StoredProcedure bilan
using (IDbConnection db =newSqlConnection(_connectionString)){var prodsedure ="[ToFullDateTime]";var values =new { dateTime =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };FullTime result =db.QuerySingleOrDefault<FullTime>(prodsedure, values,commandType:CommandType.StoredProcedure);return$"Год - {result.Year}, Четверть - {result.Quarter}, Месяц название - {result.MonthName}, День года - {result.DayOfYear}, День - {result.Day}, День недели - {result.DayOfTheWeek}, Час - {result.Hour}, Минута - {result.Minute}, Секунд - {result.Second}"; }
Demak yana bitta takrorlash qilib olamiz, boshi esingizdan chiqib ketgan bo’lsa:
a) Sql bilan bog’lanish hosil qilish
using (IDbConnection db =newSqlConnection(_connectionString)){ //qanaqadir kod}
b) Sql kodlarini shunchaki yozish
SELECT*FROM People
c) Yangi model tuzish (biz qaytarayotgan malumot obyekt ko’rinishida chiroyli bo’lishi uchun)
publicclassClass_Nomi{ //Proporties}
d) Dapper bilan ko’nikma: Agar nimanidir bazada yangilamoqchi bo’lsangiz Execute()
using (IDbConnection db =newSqlConnection(_connectionString)){var sqlQuery ="INSERT INTO People VALUES(@Id, @FullName, @Age, @Email)";db.Execute(sqlQuery, user);}
e) Dapper bilan ko’nikma: Agar nimanidir bazadan olmoqchi bo’lsangiz Query()
using (IDbConnection db =newSqlConnection(_connectionString)){returndb.Query<User>("SELECT * FROM People").ToList();}
f) Dapper bilan ko’nikma: Agar bazadan kelayotgan malumot bir qator bo’lsa QuerySingleOrDefault()
using (IDbConnection db =newSqlConnection(_connectionString)){returndb.QuerySingleOrDefault<User>("SELECT * FROM People WHERE Id = @id",new { id }) ??newUser { };}
g) Dapper bilan ko’nikma: Agar bazadan bitta prodsedureni ishlamoqchi bo’lsangiz
CommandType.StoredProcedure yoki CommandType.Text bilan qilamiz.