Need to update referenced value in generic method - how to make it cleaner?

49 Views Asked by At
  • I have to update ~30 parameters.
  • Update depends on LastUpdated field.
  • Every parameter has it's own LastUpdated field.
  • 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.

1

There are 1 best solutions below

2
Peter B On

You can use ref:

private static void UpdateSourceIfNewer<T>(ref T sourceValue, ref DateTime? sourceLastUpdate, T inputValue, DateTime? inputLastUpdate)
{
    if (inputValue != null &&
       (sourceLastUpdate is null || sourceLastUpdate < inputLastUpdate))
    {
        sourceValue = inputValue;
        sourceLastUpdate = inputLastUpdate;
    }
}

Usage:

UpdateSourceIfNewer(ref src.Param1, ref src.Param1LastUpdated, input.Param1, input.Metadata.Param1);

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 because inputLastUpdate is of type DateTime? I had to remove the ?.LastUpdated part. Maybe that is related to why you said it didn't work, but I can't figure out why or how.