1-) C# RMOS - .Net Core 8 Web Api ve Dapper Kullanımı 2 interfaces ve unitofwork
bu şekilde yazarsan swagger de gözükmez
[ApiExplorerSettings(IgnoreApi = true)]
[NonAction]
SQL BAĞLANTI HATASI ALIRSAN BUNU ÇALIŞTIR
public async Task<IEnumerable<object>> GetAllAsync4() { var notlar = await _connection.QueryAsync("SELECT * FROM notlar");
foreach (var item in notlar) { string ad = item.ad; }
return notlar;
|
github link : https://github.com/ramazanhaber/NetCore8ApiDapper
youtube link : https://youtu.be/gHdEIkYvFBM
1. kurulması gerekenler
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Dapper.Contrib
Install-Package Microsoft.AspNetCore.Authentication.JwtBearer
Install-Package Newtonsoft.Json
1. Adım Helper -> DatabaseConnections.cs
2. Adım Models -> Ogrenciler.cs
3. Adım Interfaces-> IGenericRepository.cs
4. Adım Interfaces-> IOgrencilerRepository.cs
namespace Netcore8_test.Interfaces { public interface IOgrencilerRepository : IGenericRepository<Ogrenciler> { } } |
5. Adım Interfaces-> IUnitOfWork.cs
namespace Netcore8_test.Interfaces { public interface IUnitOfWork { IOgrencilerRepository Ogrenciler { get; } } } |
6. Adım Repository-> GenericRepository.cs
7. Adım Repository-> OgrencilerRepository.cs
8. Adım Repository-> UnitOfWork.cs
9. Adım -> appsettings.json
"Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "ConnectionStrings": { "DefaultConnection": "Server=RAMBO3;Database=Deneme1;User ID=sa;Password=şifre;TrustServerCertificate=true;",
"SecondConnection": "Server=RAMBO3;Database=Deneme2;User ID=sa;Password=şifre;TrustServerCertificate=true;" }, "AllowedHosts": "*" }
|
10. Adım -> Program.cs
11. adım Controller -> OgrencilerController.cs
AYRICA BAKINIZ
açıklama : iGeneric olmayan bir metot daha yazmak istersem bu şekilde yapabilirim (GetAllAsync2 ve GetAllAsync3 şeklinde)
public interface INotlarRepository: IGenericRepository<Notlar> { Task<IEnumerable<object>> GetAllAsync2(); Task<IEnumerable<Notlar>> GetAllAsync3(); |
[ApiController] public class NotlarController : ControllerBase { private readonly IUnitOfWork unitOfWork; private readonly IDbConnection _connection;
public NotlarController(DatabaseConnections connections, IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; _connection = connections.DefaultConnection; // ilk veri tabanı
} [HttpPost] [Route("notlarilistele")] public async Task<IActionResult> notlarilistele() { var notlarler = await unitOfWork.Notlar.GetAllAsync2(); return Ok(notlarler); }
[HttpPost] [Route("notlarilistele2")] public async Task<IActionResult> notlarilistele2() { var notlarler = await unitOfWork.Notlar.GetAllAsync3(); return Ok(notlarler); }
|