ASP.NET Core 构建评论基础设施:从接口到控制器完整实战
作者:xiaoxin.gao · 2025-06-27 · 阅读时间:4分钟
ASP.NET Core CommentRepository 仓储模式
ASP.NET Core 评论模块
Entity Framework Core ToListAsync
Web API 基础架构
仓储模式 实践
如何在 .NET 8 中构建评论基础设施
引言:开启评论模块基础设施构建 在前面的章节中,我们专注于 Stock 实体的 CRUD 实现;本节教程将加速 […]
文章目录
引言:开启评论模块基础设施构建
在前面的章节中,我们专注于 Stock 实体的 CRUD 实现;本节教程将加速开发进度,实时构建 Comment 相关的基础架构,剥离控制器中的数据访问,采用仓储模式,让控制器更简洁、可维护。
一、定义仓储接口 ICommentRepository

在项目的 Repositories/Interfaces 目录下新建 ICommentRepository.cs,仅声明一条方法以快速搭建框架:
public interface ICommentRepository
{
Task < IEnumerable < Comment > > GetAllAsync();
}
二、实现 CommentRepository 与 EF Core 调用

在 Repositories 目录下创建 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 中注册依赖注入

打开 Program.cs,在 builder.Services 添加仓储实例映射:
builder.Services.AddScoped < ICommentRepository, CommentRepository > ();
确保与 DbContext 的注册顺序合理,便于控制器注入时能够解析。
四、创建 CommentController 并注入仓储

在 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();
// 后续映射至 DTO
return Ok(commentsDto);
}
}
五、定义 CommentDto 简化返回结构

在 Models/Dto 目录下新建 CommentDto.cs,复制实体字段并去除导航属性:
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; }
}
六、实现 CommentMapper 完成实体到 DTO 映射

新建静态映射类 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
};
}
在控制器中,将仓储返回的实体集合通过 LINQ 映射:

var commentsDto = comments.Select(s = > s.ToCommentDto());
七、添加测试数据并验证基础 GET 接口

- 在 SQL Server Management Studio 中向
Comments表插入几条测试记录(如 StockId=21)。 -
运行
dotnet watch run,打开 Swagger UI,执行 GET /api/comment:- 确认返回的 JSON 结构为
CommentDto列表。 - 若一切正常,即表示基础架构搭建完成,可继续扩展 POST/PUT/DELETE 等操作。
- 确认返回的 JSON 结构为
小结
- 通过仓储模式剥离控制器与数据库交互逻辑。
- 使用依赖注入注册仓储实现,保证松耦合。
- 定义DTO 与静态映射,确保 API 返回结构简洁。
- 完成基础 GET 接口并验证,可作为后续 CRUD 功能的坚实基础。
原文引自YouTube视频:https://www.youtube.com/watch?v=A3tdyk68KAw
热门推荐
一个账号试用1000+ API
助力AI无缝链接物理世界 · 无需多次注册
3000+提示词助力AI大模型
和专业工程师共享工作效率翻倍的秘密
最新文章
- OWASP API十大漏洞及DAST如何保护您 …
- API安全在物联网(IoT)中的关键作用
- Java后端API接口开发规范
- PyJWT:轻松搞定Token认证,让你的API更安全!
- 2025年7月GitHub 上热门的10大API开源项目
- 构建远程医疗应用的10个最佳Telehealth API选项
- 使用 FastAPI、Docker 和 Hugging Face Transformers 的文本分类 API
- IdeaGitLab 插件API Token is not valid解决方案
- API架构设计基础
- 什么是GPT-4?完整指南
- 最佳API测试工具:REST和SOAP自动化 – Parasoft
- 如何使用Ollama(完整Ollama速查表)- Apidog