- I have to update ~30 parameters.
- Update depends on
LastUpdatedfield. - Every parameter has it's own
LastUpdatedfield. - This is an ETL process for IoT Device Twin (it means that I cannot change the input JSON), so I'd like to make it as fast as possible (without unnecessary parsings).
Here is the generic method:
private static (T value, DateTime? lastUpdate) UpdateSourceIfNewer<T>(T sourceValue, DateTime? sourceLastUpdate, T inputValue, DateTime? inputLastUpdate)
{
if (inputValue != null &&
(sourceLastUpdate is null || sourceLastUpdate < inputLastUpdate))
{
sourceValue = inputValue;
sourceLastUpdate = inputLastUpdate?.LastUpdated;
}
return (sourceValue, sourceLastUpdate);
But this method requires code which is not very clean for me (below). Still better than a wall of ifs, but is there any way to make it cleaner?
(src.Param1, src.Param1LastUpdated) = UpdateSourceIfNewer(
src.Param1, src.Param1LastUpdated, input.Param1, input.Metadata.Param1);
(src.Param2, src.Param2LastUpdated) = UpdateSourceIfNewer(
src.Param2, src.Param2LastUpdated, input.Param2, input.Metadata.Param2);
/// another ~30 parameters
EDIT: ref won't work. When sourceValue is null, the change is not propagated.
You can use ref:
Usage:
EDIT: After you said it didn't work, I created a DotNetFiddle which I believe shows that it does work, for all scenarios that I could think of (it always prints that all values are the same).
In doing so I was forced to make a fix to the code because I copied your use of
inputLastUpdate?.LastUpdated, but becauseinputLastUpdateis of typeDateTime?I had to remove the?.LastUpdatedpart. Maybe that is related to why you said it didn't work, but I can't figure out why or how.