Files
satellite-provider/SatelliteProvider.DataAccess/TypeHandlers/EnumStringTypeHandler.cs
T
Oleksandr Bezdieniezhnykh e9d6db077c [AZ-484] Fix multi-source tile reads: drop Dapper enum handler
Two integration-test failures uncovered after the initial commit:

1) GetTilesByRegionAsync outer ORDER BY referenced 'updated_at' but
   the inner DISTINCT ON subquery aliased it to 'UpdatedAt' (Postgres
   folds to 'updatedat'). DISTINCT ON already guarantees one row per
   (latitude, longitude, ...) so the third tiebreak was unreachable;
   removed it.

2) Dapper 2.1.35 silently bypasses SqlMapper.TypeHandler<T> for enum
   types during read deserialization (Dapper issue #259). The
   TileSourceTypeHandler worked for writes but reads fell through to
   Enum.TryParse, which cannot map 'google_maps' to GoogleMaps.

   Pivoted: TileEntity.Source is now a string (the wire value).
   TileSource enum stays as the public producer surface in
   Common.Enums; TileSourceConverter (Common.Enums) provides
   ToWireValue / FromWireValue / IsValidWireValue at the boundary.
   TileSourceTypeHandler deleted; registration removed from
   DapperEnumTypeHandlers.RegisterAll.

   tile-storage.md Inv-5 amended to document the storage choice.
   _docs/LESSONS.md L-001 records the Dapper bypass for future cycles.

Full suite passes (213 unit + integration suite incl. AZ-484
AC-1..AC-5, security SEC-01..SEC-04, AZ-356/362/357).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 06:44:34 +03:00

52 lines
1.4 KiB
C#

using System.Data;
using Dapper;
using SatelliteProvider.Common.Enums;
namespace SatelliteProvider.DataAccess.TypeHandlers;
public class EnumStringTypeHandler<T> : SqlMapper.TypeHandler<T> where T : struct, Enum
{
public override T Parse(object value)
{
if (value is null || value is DBNull)
{
throw new DataException($"Cannot parse null DB value into enum {typeof(T).Name}");
}
var s = value as string ?? value.ToString();
if (string.IsNullOrEmpty(s))
{
throw new DataException($"Cannot parse empty DB value into enum {typeof(T).Name}");
}
if (!Enum.TryParse<T>(s, ignoreCase: true, out var parsed) || !Enum.IsDefined(parsed))
{
throw new DataException($"DB value '{s}' is not a defined member of enum {typeof(T).Name}");
}
return parsed;
}
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.Value = value.ToString().ToLowerInvariant();
parameter.DbType = DbType.String;
}
}
public static class DapperEnumTypeHandlers
{
private static int _registered;
public static void RegisterAll()
{
if (Interlocked.Exchange(ref _registered, 1) == 1)
{
return;
}
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RegionStatus>());
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RoutePointType>());
}
}