brucezhang80 / dapper-dot-net

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

Should dbNull cast to default valuetype value? #94

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run a query that should return a datatype that is a valuetype, but returns 
nulls instead.

/* returns one or more rows with a single column of null value */
SELECT
  null AS aGuid
FROM
  someTable

2. Expect to return an enumerable of Guids all set to default value.

var someGuids = multi.Read<Guid>().ToArray();
Console.Write(someGuids.First());

What is the expected output? What do you see instead?
Expected output should be "0000-0000...".
Do not see anything, exception inside GetStructDeserializer.

What version of the product are you using? On what operating system?
Latest?

Please provide any additional information below.
Should we expect that non-nullable valuetypes come back as the default value 
for the valuetype?

Maybe change GetStructDeserializer from:
    var val = r.GetValue(index);
    return val is DBNull ? null : val;
To:
    var valType = r.GetFieldType(index);
    var val = r.GetValue(index);
    return val is DBNull
        ? (valType.IsValueType
            ? Activator.CreateInstance(valType)
            : null)
        : val;

This quick fix could be faster using something besides Activator.CreateInstance.

Original issue reported on code.google.com by Nick...@gmail.com on 27 Apr 2012 at 9:28