Dapper build 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 is deserialized from a JSON string. Therefore, some of the properties null

.

What are the best ways to create a list of options for going to Dapper.

Currently my logic for building the params string for the tag at the end of the SQL query is as follows:

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 help parametersObj

, I went with a JavaScript

way to dynamically create an object. If I did it using a dynamic

instead of an object - would it still work?

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);

      

+3


source to share


2 answers


I think the second example will work when you change

dynamic parametersObj = new {};

      

to

dynamic parametersObj = new ExpandoObject();

      

and request



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

      

NOTE: filling a dynamic object like

conn.QueryAsync(sqlString, parametersObj);

      

will cause an error

Extension methods cannot be dispatched dynamically

+2


source


You don't need anything: just pass your object as parameters. Dapper will only pass properties / parameters that it can identify in the request ... and even if it passed all of them: it understands null.

The facility is beautiful.



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

      

+2


source







All Articles