简体   繁体   English

将 Dapper 与 SQL 服务器一起使用

[英]Using Dapper with SQL Server

I am currently developing an application using C# (Visual Studio 2019)and SQL Server 2017 using Dapper.我目前正在使用 C# (Visual Studio 2019) 和 SQL Server 2017 使用 Dapper 开发应用程序。 Below is a routine that works fine right now to execute a stored procedure in SQL Server.下面是一个现在可以正常工作的例程,可以在 SQL 服务器中执行存储过程。

The C# code uses the MVVM framework, and I currently have 60 model classes to map the tables in the SQL Server database. The C# code uses the MVVM framework, and I currently have 60 model classes to map the tables in the SQL Server database. There are about 120 stored procedures that produce results that are mapped to the model classes using Dapper.大约有 120 个存储过程产生结果,这些结果使用 Dapper 映射到 model 类。

Consider the following code snippet as pseudocode (but it actually works to execute the stored procedure and return the correct results).将以下代码片段视为伪代码(但它实际上可以执行存储过程并返回正确的结果)。

public List<SomeDefinedModel> ExecuteSQLSPROC(string SPROCName, SqlParameter[] parameters)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(DBHelper.CNNVal("MYLocalServer")))
    {
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
        command.Connection = (SqlConnection)connection;
        command.CommandText = SPROCName;

        DynamicParameters parms = new DynamicParameters();

        for (int i = 0; i < parameters.Length; i++)
        {
            var parmname = parameters[i].ParameterName;
            var parmvalue = parameters[i].Value;
            parms.Add(parmname, parmvalue);
        }

        var output = connection.Query<SomeDefinedModel>(SPROCName, parms, commandType: CommandType.StoredProcedure).ToList();

        return output;
    }
}

I want to change this routine so the routine so that it method signature is as follows:我想更改此例程,以便该例程使其方法签名如下:

public List<TheNameOfTheModelToMap> ExecuteSQLSPROC(string SPROCName, SqlParameter[] parameters, TheNameOfTheModelToMap)

That is, the designation of the model to which Dapper is to map is a variable - it can be any valid model.也就是说,Dapper 对 map 的 model 的名称是一个变量 - 它可以是任何有效的 model。 I have define the variable TheNameOfTheModelToMap as type object, and the return value as var, as List<object> , and so forth.我已将变量TheNameOfTheModelToMap定义为 object 类型,并将返回值定义为 var、 List<object>等等。 They produce errors because of this syntax:由于以下语法,它们会产生错误:

var output = connection.Query<TheNameOfTheModelToMap>(SPROCName, parms, commandType: CommandType.StoredProcedure).ToList();

I want to comply with the principles of DRY and code just one method to accept ANY model designation, ANY stored procedure designation and have Dapper map the query results.我想遵守 DRY 的原则,只编写一种方法来接受 ANY model 指定、任何存储过程指定并拥有 Dapper map 查询结果。 Any possibilities?有什么可能吗?

I think you just need one generic method like so:我认为您只需要一种像这样的通用方法:


public List<T> ExecuteSQLSPROC<T>(string SPROCName, SqlParameter[] parameters)
{
    using (var connection = new System.Data.SqlClient.SqlConnection(DBHelper.CNNVal("MYLocalServer")))
    {
        var parms = new DynamicParameters();
        foreach (var parameter in parameters)
            parms.Add(parameter.ParameterName, parameter.Value);

        var output = connection.Query<T>(
                SPROCName,
                parms,
                commandType: CommandType.StoredProcedure
            )
            .ToList();

        return output;
    }
}


And every other method becomes a specialized invoke of it:并且所有其他方法都成为它的专门调用:

public List<SomeDefinedModel> ExecuteOneProc(SqlParameter[] parameters)
  => ExecuteSQLSPROC<SomeDefinedModel>("OneProcName", parameters);

Is this ok for your use case?这适合您的用例吗? If you need one procedure for single values (which would not use Dapper's Query method) you would just create specialized variations of the ExecuteSQLSPROC like ExecuteStoredProcSingleRow or something along those lines.如果您需要一个用于单个值的过程(不使用 Dapper 的Query方法),您只需创建ExecuteStoredProcSingleRow ExecuteSQLSPROC类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM