Правки по результатам ревью

This commit is contained in:
Roman Efremov 2025-02-28 13:15:27 +05:00
parent 2dca1cc722
commit 1e2fa287a0
5 changed files with 38 additions and 13 deletions

View File

@ -13,6 +13,7 @@ public class SetpointMappingClient : ISetpointMappingClient
private readonly ISetpointClient setpointClient;
private readonly MappingConfig mappingConfigs;
/// <inheritdoc/>
public SetpointMappingClient(ISetpointClient setpointClient, MappingConfig mappingConfigs)
{
this.setpointClient = setpointClient;
@ -62,11 +63,11 @@ public class SetpointMappingClient : ISetpointMappingClient
/// <inheritdoc/>
public async Task<IEnumerable<SetpointLogDto>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
{
var res = await setpointClient.GetPart(dateBegin, take, token);
var result = await setpointClient.GetPart(dateBegin, take, token);
DeserializeList(res);
DeserializeList(result);
return res;
return result;
}
/// <inheritdoc/>
@ -81,6 +82,7 @@ public class SetpointMappingClient : ISetpointMappingClient
public void Dispose()
{
setpointClient.Dispose();
}
private object DeserializeValue(Guid key, JsonElement value)

View File

@ -2,6 +2,7 @@
using DD.Persistence.Client.Clients.Mapping.Abstractions;
using DD.Persistence.Models;
using DD.Persistence.Models.Common;
using Microsoft.Extensions.Logging;
namespace DD.Persistence.Client.Clients.Mapping.Clients;

View File

@ -1,5 +1,6 @@
using DD.Persistence.Client.Clients.Mapping.Abstractions;
using DD.Persistence.Models.Configurations;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
@ -8,22 +9,25 @@ internal class MapperStorage : IMapperStorage
{
private readonly ConcurrentDictionary<Guid, TimestampedSetMapper> mapperCache = new();
private readonly MappingConfig mappingConfigs;
public MapperStorage(MappingConfig mappingConfigs)
private readonly ILogger<TimestampedSetMapper> logger;
public MapperStorage(MappingConfig mappingConfigs, ILogger<TimestampedSetMapper> logger)
{
this.mappingConfigs = mappingConfigs;
this.logger = logger;
}
public TimestampedSetMapper? GetMapper(Guid idDiscriminator)
{
if(mappingConfigs.TryGetValue(idDiscriminator, out var type))
return mapperCache.GetOrAdd(idDiscriminator, name => new TimestampedSetMapper(idDiscriminator, type));
if (mappingConfigs.TryGetValue(idDiscriminator, out var type))
return mapperCache.GetOrAdd(idDiscriminator, name => new TimestampedSetMapper(idDiscriminator, type, logger));
return null;
}
public TimestampedSetMapper GetMapper<T>(Guid idDiscriminator)
{
return mapperCache.GetOrAdd(idDiscriminator, name => new TimestampedSetMapper(idDiscriminator, typeof(T)));
return mapperCache.GetOrAdd(idDiscriminator, name => new TimestampedSetMapper(idDiscriminator, typeof(T), logger));
}
}

View File

@ -1,4 +1,5 @@
using DD.Persistence.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
@ -9,13 +10,16 @@ namespace DD.Persistence.Client.Clients.Mapping;
internal class TimestampedSetMapper
{
private readonly Type entityType;
private readonly ILogger<TimestampedSetMapper> logger;
public Guid IdDiscriminator { get; }
private readonly ConcurrentDictionary<string, PropertyInfo?> PropertyCache = new();
public TimestampedSetMapper(Guid idDiscriminator, Type entityType)
public TimestampedSetMapper(Guid idDiscriminator, Type entityType, ILogger<TimestampedSetMapper> logger)
{
IdDiscriminator = idDiscriminator;
this.entityType = entityType;
this.logger = logger;
}
public object DeserializeTimeStampedData(TimestampedValuesDto data)
@ -66,7 +70,10 @@ internal class TimestampedSetMapper
var value = element.Deserialize(property.PropertyType);
property.SetValue(entity, value);
}
catch (Exception) { }
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
private void SetPropertyValueForStruct(ref object entity, string propertyName, object value)
{
@ -79,7 +86,10 @@ internal class TimestampedSetMapper
var convertedValue = Convert.ChangeType(value, property.PropertyType);
property.SetValue(entity, convertedValue);
}
catch (Exception) { }
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
@ -95,7 +105,10 @@ internal class TimestampedSetMapper
var value = jsonElement.Deserialize(property.PropertyType);
property.SetValue(entity, value);
}
catch (Exception) { }
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
private void SetPropertyValue(ref object entity, string propertyName, object value)
@ -109,7 +122,10 @@ internal class TimestampedSetMapper
var convertedValue = Convert.ChangeType(value, property.PropertyType);
property.SetValue(entity, convertedValue);
}
catch (Exception) { }
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
private PropertyInfo? GetPropertyInfo(string propertyName)

View File

@ -3,6 +3,7 @@ using DD.Persistence.Client.Clients.Mapping;
using DD.Persistence.Client.Clients.Mapping.Clients;
using DD.Persistence.Models;
using DD.Persistence.Models.Configurations;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System.Text.Json;
@ -15,6 +16,7 @@ public record SecondTestDto(Guid DiscriminatorId, DateTimeOffset Timestamp, int
public class MappingClientsTest
{
private readonly ITimestampedValuesClient timestampedValuesClient = Substitute.For<ITimestampedValuesClient>();
private readonly ILogger<TimestampedSetMapper> logger = Substitute.For<ILogger<TimestampedSetMapper>>();
private readonly TimestampedMappingClient timestampedMappingClient;
private readonly MappingConfig mappingConfigs;
@ -23,7 +25,7 @@ public class MappingClientsTest
public MappingClientsTest()
{
mappingConfigs = GetConfig();
var storage = new MapperStorage(mappingConfigs);
var storage = new MapperStorage(mappingConfigs, logger);
timestampedMappingClient = new TimestampedMappingClient(timestampedValuesClient, storage);
}