-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Conversation
… 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.)
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]; |
There was a problem hiding this comment.
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 :)
…ype in T when T is an interface.
Added in tests for GetAndGetAllWithNullableValues and GetAsyncAndGetAllAsyncWithNullableValues.
Added comment to identify which issue the test relates to.
Adjusted variable names in GetAndGetAllWithNullableValues and GetAsyncAndGetAllAsyncWithNullableValues.
@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. |
@JaseRandall looking awesome, thank you so much for your first contribution! Excellently done :) |
This pull request is in response to an issue that I raised:
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
OR
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.