.NET6之MiniAPI(二十五):Dapper

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.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

以上是关于.NET6之MiniAPI(二十五):Dapper的主要内容,如果未能解决你的问题,请参考以下文章

.NET6之MiniAPI(二十一):限流

.NET6之MiniAPI(十五):跨域CORS(下)

.NET6之MiniAPI(二十):实体验证FluentValidation

.NET6之MiniAPI(二十二):HttpClient

.NET6之MiniAPI(二十三):Refit

.NET6之MiniAPI(二十四):用Polly重试