.NET6之MiniAPI(二十五):Dapper
注:如果你使用Dapper一段时间了,这篇你可以跳过去了。
在第十二篇中,我们聊过官方的ORM——EntityFramework,它可以把SQL细节都隐藏,这对于一些标准化的关系数据库项目非常便捷。今天,介绍另外一个流行的ORM——Dapper,Dapper是通过扩展IDbConnection方法来达到对数据的处理的,特点是灵活,高效。
还是通过项目看看Dapper的使用。首先安装两个NuGet包,我们还是使用之前的Exam项目的表来说明Dapper使用,库是SQL Server。
安装Dapper和SqlClient:
Install-Package Dapper
Install-Package Microsoft.Data.SqlClient
下面是对Question进行增删改查
using Dapper;
using Microsoft.Data.SqlClient;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IQuestionService, QuestionService>();
var app = builder.Build();
app.MapPost("/question", async (IQuestionService questionService, QuestionModel question) =>
{
return await questionService.AddQuestionAsync(question);
});
app.MapPut("/question", async (IQuestionService questionService, QuestionModel question) =>
{
return await questionService.ModifyQuestionAsync(question);
});
app.MapGet("/question/{id}", async (IQuestionService questionService, int id) =>
{
return await questionService.GetQuestionAsync(id);
});
app.MapDelete("/question/{id}", async (IQuestionService questionService, int id) =>
{
return await questionService.DeleteQuestionAsync(id);
});
app.Run();
public interface IQuestionService
{
Task<QuestionModel> GetQuestionAsync(int id);
Task<bool> AddQuestionAsync(QuestionModel question);
Task<bool> DeleteQuestionAsync(int id);
Task<bool> ModifyQuestionAsync(QuestionModel question);
}
public class QuestionService : IQuestionService
{
private readonly SqlConnection _connection;
public QuestionService(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("ExamDatabase");
_connection = new SqlConnection(connectionString);
}
public async Task<bool> AddQuestionAsync(QuestionModel question)
{
var sql = @"INSERT INTO [Questions]
([Question]
,[Score]
,[QuestionTypeID]
,[SujectTypeID])
VALUES
(@Question
,@Score
,@QuestionTypeID
,@SujectTypeID)";
return (await _connection.ExecuteAsync(sql, question)) > 0;
}
public async Task<bool> DeleteQuestionAsync(int id)
{
var sql = @"delete from questions where id=@id";
return (await _connection.ExecuteAsync(sql, new { id })) > 0;
}
public async Task<QuestionModel> GetQuestionAsync(int id)
{
var sql = @"select * from questions where id=@id";
return await _connection.QuerySingleAsync<QuestionModel>(sql, new { id });
}
public async Task<bool> ModifyQuestionAsync(QuestionModel question)
{
var sql = @"UPDATE [dbo].[Questions]
SET [Question] = @Question
,[Score] = @Score
,[QuestionTypeID] = @QuestionTypeID
,[SujectTypeID] = @SujectTypeID
WHERE ID=@ID";
return (await _connection.ExecuteAsync(sql, question)) > 0;
}
}
通过上面事例,可以看到Dapper通过扩展IDbConnection的扩展方法来提供功能,基本上查询用Query<T>(sql,Param,...),增删改用Execute(sql,T,...),Dapper的方便之处是T全部是自运映射到sql中的参数的,这样就能做到sql我们可以灵活且更高效语法编写,参数可以方便的进出sql,达到一个平衡点。如下图示:
关于更丰富多彩的Dapper使用,详见Github仓库说明文档:https://github.com/DapperLib/Dapper
想要更快更方便的了解相关知识,可以关注微信公众号
****欢迎关注我的asp.net core系统课程****
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524