Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to GetAll to allow the usage of a nullable type in T when T is an interface… #933 #936

Merged
merged 8 commits into from
Mar 9, 2018

Conversation

JaseRandall
Copy link
Contributor

This pull request is in response to an issue that I raised:

'Null object cannot be converted to a value type.' when using nullable type on interface #933

Basically I had DateTime? and was using an interface so that I could use the change tracking in Dapper.Contrib.

Without this modification, the code throws

System.InvalidCastException: 'Invalid cast from 'System.String' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.'

OR

'Null object cannot be converted to a value type.'

depending on whether the value from the (SQLite) database has a value or is null.

My suggested change:
checks for null and if so, continues;
otherwise if the type is nullable, it obtains the underlying property and assigns the value to the underlying property;
otherwise assign the value to the property as per the original code.

… an interface: further change to check if val == null and if so then don't try to assign it as a property value.
… interface. (effectively the same change as the previous change to GetAll.)
@NickCraver
Copy link
Member

NickCraver commented Mar 7, 2018

Can you please add a test for this to ensure we don't regress?

if (val == null) continue;
if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var genericType = property.PropertyType.GetGenericArguments()[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since .GetGenericArguments() allocates an array every time, instead use: Nullable.GetUnderlyingType(property.PropertyType) to do this much cheaper. Same below :)

Added in tests for GetAndGetAllWithNullableValues and  GetAsyncAndGetAllAsyncWithNullableValues.
Added comment to identify which issue the test relates to.
Adjusted variable names in GetAndGetAllWithNullableValues and GetAsyncAndGetAllAsyncWithNullableValues.
@JaseRandall
Copy link
Contributor Author

@NickCraver I realised that I missed including the async Get & GetAll methods. I've added the additional code to those methods and have added a couple of tests. I'll admit that this is the first time I've used tests (I'm such an amatuer!!) and so while I think they are adequete, I'm not certain of that. Hopefully it's all up to scratch now but if not, please let me know.

@NickCraver
Copy link
Member

@JaseRandall looking awesome, thank you so much for your first contribution! Excellently done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants