yuan8825399 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

Inconvenience: string size not correctly set for output when no string or size is specified #167

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is not really an issue but rather an inconvenience. 

I only had an Oracle db to test against, so I'm not sure the same behavior 
holds everywhere.

The 'issue' is with the resolution of the size of output strings.

var p = new DynamicParameters();
p.Add("somedata", dbType: DbType.String, direction: ParameterDirection.Output);

When you use the above syntax, nothing will be returned as the length of the 
return string will be considered to be 0 by Dapper.

Two workarounds exist:
1) Provide the empty string as value. This results in the existing code to set 
the size to 4000.

        p.Add("somedata", value: String.Empty, dbType: DbType.String, direction: ParameterDirection.Output);

2) Provide a size. This results in the existing code to set the size to 3000 
for the example below.

        p.Add("somedata", dbType: DbType.String, size: 3000, direction: ParameterDirection.Output);

But I think Dapper should handle this automatically and not require the user to 
provide either. So I'm suggesting to add an extra condition along the following 
lines:

        // EXISTING CODE
        var val = param.Value;
        p.Value = val ?? DBNull.Value;
        p.Direction = param.ParameterDirection;
        var s = val as string;
        if (s != null)
        {
          if (s.Length <= 4000)
          {
            p.Size = 4000;
          }
        }
        if (param.Size != null)
        {
          p.Size = param.Size.Value;
        }
        // Existing code
        // New code (might need to be more generic)
        if (   s == null 
            && param.Size == null 
            && param.DbType == DbType.String 
            && param.ParameterDirection == ParameterDirection.Output)
        {
          p.Size = 4000;
        }
        // New code

Original issue reported on code.google.com by yves.dhondt@gmail.com on 6 Jan 2014 at 7:59