LastUpdated
字段。LastUpdated
字段。这是通用方法:
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);
但是这个方法需要的代码对我来说不是很干净(如下)。仍然比一堵 ifs 墙好,但是有什么办法让它更干净吗?
(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
您可以使用参考:
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?.LastUpdated;
}
}
用途:
UpdateSourceIfNewer(ref src.Param1, ref src.Param1LastUpdated, input.Param1, input.Metadata.Param1);