Integrate NetTopologySuite and create CoordinateData mapping (#250)

This commit is contained in:
2026-08-26 01:10:09 -04:00
committed by GitHub
parent ad1c09f139
commit 4ca35de7ef
13 changed files with 98 additions and 38 deletions

View File

@@ -17,9 +17,16 @@ namespace Features.Breweries.Repository;
/// yields the UDT's serialized bytes as a plain <c>varbinary</c> value that Dapper can bind directly.
/// </remarks>
public class BreweryRepository(ISqlConnectionFactory connectionFactory)
: Database.Connection.DapperRepository(connectionFactory),
: DapperRepository(connectionFactory),
IBreweryRepository
{
private const int GeographySrid = 4326;
static BreweryRepository()
{
SqlMapper.AddTypeHandler(new CoordinateDataTypeHandler());
}
/// <inheritdoc/>
public async Task<BreweryPost?> GetByIdAsync(Guid id)
{
@@ -226,7 +233,10 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
AddressLine1 = @AddressLine1,
AddressLine2 = @AddressLine2,
PostalCode = @PostalCode,
Coordinates = @Coordinates
Coordinates = CASE
WHEN @Latitude IS NULL THEN NULL
ELSE geography::Point(@Latitude, @Longitude, @Srid)
END
WHERE BreweryPostID = @BreweryPostId
""",
new
@@ -236,7 +246,9 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
brewery.Location.AddressLine1,
brewery.Location.AddressLine2,
brewery.Location.PostalCode,
brewery.Location.Coordinates,
Latitude = brewery.Location.Coordinates?.Latitude,
Longitude = brewery.Location.Coordinates?.Longitude,
Srid = GeographySrid,
},
transaction
)
@@ -260,7 +272,10 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
@AddressLine1,
@AddressLine2,
@PostalCode,
@Coordinates)
CASE
WHEN @Latitude IS NULL THEN NULL
ELSE geography::Point(@Latitude, @Longitude, @Srid)
END)
""",
new
{
@@ -270,7 +285,9 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
brewery.Location.AddressLine1,
brewery.Location.AddressLine2,
brewery.Location.PostalCode,
brewery.Location.Coordinates,
Latitude = brewery.Location.Coordinates?.Latitude,
Longitude = brewery.Location.Coordinates?.Longitude,
Srid = GeographySrid,
},
transaction
)
@@ -394,7 +411,10 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
@AddressLine1,
@AddressLine2,
@PostalCode,
@Coordinates)
CASE
WHEN @Latitude IS NULL THEN NULL
ELSE geography::Point(@Latitude, @Longitude, @Srid)
END)
""",
new
{
@@ -404,7 +424,9 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
brewery.Location.AddressLine1,
brewery.Location.AddressLine2,
brewery.Location.PostalCode,
brewery.Location.Coordinates,
Latitude = brewery.Location.Coordinates?.Latitude,
Longitude = brewery.Location.Coordinates?.Longitude,
Srid = GeographySrid,
},
transaction
)

View File

@@ -0,0 +1,29 @@
using System.Data;
using Dapper;
using Domain.Entities;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
namespace Features.Breweries.Repository;
/// <summary>
/// Dapper <see cref="SqlMapper.TypeHandler{T}" /> that deserializes a SQL Server <c>GEOGRAPHY</c>
/// column (read as <c>varbinary(max)</c>, see <see cref="BreweryRepository" />) into a
/// <see cref="CoordinateData" />.
/// </summary>
public sealed class CoordinateDataTypeHandler : SqlMapper.TypeHandler<CoordinateData>
{
private readonly SqlServerBytesReader _reader = new() { IsGeography = true };
public override CoordinateData Parse(object value)
{
var point = (Point)_reader.Read((byte[])value);
return new CoordinateData(point.Y, point.X);
}
public override void SetValue(IDbDataParameter parameter, CoordinateData? value) =>
throw new NotSupportedException(
$"{nameof(CoordinateData)} cannot be bound as a query parameter directly; "
+ "pass Latitude/Longitude and build the GEOGRAPHY value in SQL text."
);
}