一、定義倉(cāng)儲(chǔ)接口 ICommentRepository

在項(xiàng)目的 Repositories/Interfaces 目錄下新建 ICommentRepository.cs,僅聲明一條方法以快速搭建框架:

public interface ICommentRepository
{
    Task < IEnumerable < Comment > > GetAllAsync();
}

二、實(shí)現(xiàn) CommentRepository 與 EF Core 調(diào)用

Repositories 目錄下創(chuàng)建 CommentRepository.cs,繼承 ICommentRepository 并注入 ApplicationDbContext

public class CommentRepository : ICommentRepository
{
    private readonly ApplicationDbContext _context;
    public CommentRepository(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task < IEnumerable < Comment > > GetAllAsync()
    {
        return await _context.Comments.ToListAsync();
    }
}

三、在 Program.cs 中注冊(cè)依賴注入

打開 Program.cs,在 builder.Services 添加倉(cāng)儲(chǔ)實(shí)例映射:

builder.Services.AddScoped < ICommentRepository, CommentRepository > ();

確保與 DbContext 的注冊(cè)順序合理,便于控制器注入時(shí)能夠解析。


四、創(chuàng)建 CommentController 并注入倉(cāng)儲(chǔ)

Controllers 文件夾下新增 CommentController.cs

[ApiController]
[Route("api/[controller]")]
public class CommentController : ControllerBase
{
    private readonly ICommentRepository _commentRepo;
    public CommentController(ICommentRepository commentRepo)
    {
        _commentRepo = commentRepo;
    }

    [HttpGet]
    public async Task < IActionResult > GetAllAsync()
    {
        var comments = await _commentRepo.GetAllAsync();
        // 后續(xù)映射至 DTO
        return Ok(commentsDto);
    }
}

五、定義 CommentDto 簡(jiǎn)化返回結(jié)構(gòu)

Models/Dto 目錄下新建 CommentDto.cs,復(fù)制實(shí)體字段并去除導(dǎo)航屬性:

public class CommentDto
{
    public int    Id        { get; set; }
    public string Title     { get; set; }
    public string Content   { get; set; }
    public DateTime CreatedOn { get; set; }
    public int    StockId   { get; set; }
}

六、實(shí)現(xiàn) CommentMapper 完成實(shí)體到 DTO 映射

新建靜態(tài)映射類 CommentMapper.cs

public static class CommentMapper
{
    public static CommentDto ToDto(this Comment model) => new()
    {
        Id        = model.Id,
        Title     = model.Title,
        Content   = model.Content,
        CreatedOn = model.CreatedOn,
        StockId   = model.StockId
    };
}

在控制器中,將倉(cāng)儲(chǔ)返回的實(shí)體集合通過 LINQ 映射:

var commentsDto = comments.Select(s = > s.ToCommentDto());

七、添加測(cè)試數(shù)據(jù)并驗(yàn)證基礎(chǔ) GET 接口

  1. 在 SQL Server Management Studio 中向 Comments 表插入幾條測(cè)試記錄(如 StockId=21)。
  2. 運(yùn)行 dotnet watch run,打開 Swagger UI,執(zhí)行 GET /api/comment


小結(jié)

原文引自YouTube視頻:https://www.youtube.com/watch?v=A3tdyk68KAw

上一篇:

如何使用 ASP.NET Core 實(shí)現(xiàn)評(píng)論更新功能:完整實(shí)戰(zhàn)講解

下一篇:

ASP.NET Core Web API 模型(Models)詳解:從 POCO 到數(shù)據(jù)庫(kù)表的一站式指南
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)