简体   繁体   English

精巧的建筑参数列表

[英]Dapper building parameter list

I have this class: 我有这门课:

public class Parameters
{
    public string UserId {get;set;}
    public string OrgId {get;set;}
    public string Roles {get;set;}
}

It gets deserialised from a JSON string. 它从JSON字符串反序列化。 So some of the properties are null . 所以一些属性为null

What are the best ways to build up the params list to pass to Dapper. 建立params列表以传递给Dapper的最佳方法是什么?

At the moment my logic for building up the params string to tag on the end of the SQL statement goes like this : 目前,我在SQL语句末尾构建params字符串的逻辑如下所示:

var parameters = string.Empty;
var parametersObj = new { };
if (query.Parameters != null)
{
    if (!string.IsNullOrWhiteSpace(query.Parameters.UserId))
    {
        parameters = string.Format("{0} UserId = @UserId", parameters);
        // parametersObj.UserId = 
    }

    if (!string.IsNullOrWhiteSpace(query.Parameters.OrganisationIdentifier))
    {
        parameters = string.Format("{0}, OrganisationIdentifier = @OrganisationIdentifier", parameters);
    }

    if (!string.IsNullOrWhiteSpace(query.Parameters.Roles))
    {
        parameters = string.Format("{0}, Roles = @Roles", parameters);
    }
}

var sqlString = string.Format("exec {0} {1}", query.DbObjectName, parameters);

conn.QueryAsync<dynamic>(sqlString, )

As you can see with the parametersObj I was going with the JavaScript way of dynamically building an object. 正如您在parametersObj看到的那样,我采用了动态构建对象的JavaScript方式。 If I did do this with dynamic instead of an object - will it still work? 如果我用dynamic而不是对象做到这一点 - 它仍然可以工作吗?

example: 例:

var parameters = string.Empty;
dynamic parametersObj = new { };
if (query.Parameters != null)
{
    if (!string.IsNullOrWhiteSpace(query.Parameters.UserId))
    {
        parameters = string.Format("{0} UserId = @UserId", parameters);
        parametersObj.UserId = query.Parameters.UserId;
    }

    if (!string.IsNullOrWhiteSpace(query.Parameters.OrganisationIdentifier))
    {
        parameters = string.Format("{0} OrganisationIdentifier = @OrganisationIdentifier ", parameters);
        parametersObj.OrganisationIdentifier= query.Parameters.OrganisationIdentifier;
    }

    if (!string.IsNullOrWhiteSpace(query.Parameters.Roles))
    {
        parameters = string.Format("{0} Roles = @Roles", parameters);
        parametersObj.Roles= query.Parameters.Roles;
    }
}

var sqlString = string.Format("exec {0} {1}", query.DbObjectName, parameters);

conn.QueryAsync<dynamic>(sqlString, parametersObj);

I think the second example will work when you change 我认为第二个例子将在您改变时起作用

dynamic parametersObj = new {};

to

dynamic parametersObj = new ExpandoObject();

and the query to 和查询

conn.QueryAsync(sqlString, new 
{
    UserId = parametersObj.UserId,
    ...
};

NOTE: filling in the dynamic object like 注意:填写动态对象就好

conn.QueryAsync(sqlString, parametersObj);

will raise the error 会引起错误

Extension methods cannot be dynamically dispatched 无法动态分派扩展方法

You don't need to do anything: just pass your object as the parameters . 您无需执行任何操作:只需将对象作为参数传递即可。 Dapper will only pass in properties/parameters that it can identify in the query... and even if it passed them all in: it understands null. Dapper只传递它可以在查询中识别的属性/参数......即使它全部传递它们:它理解为null。

The object is fine. 对象很好。

...QueryAsync(sql, query.Parameters)...

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

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