GeometryToMySqlGeometryPropertyHandler

A property handler that maps the MariaDB spatial types into a MySqlGeometry property.

A property handler that maps the MariaDB spatial types (GEOMETRY, POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON and GEOMETRYCOLLECTION) into a MySqlGeometry property of the MySql.Data based connector of the package.

Overview

Name Description
Class GeometryToMySqlGeometryPropertyHandler
Namespace RepoDb.PropertyHandlers.MariaDb
Package RepoDb.MariaDb
Implements IPropertyHandler<object, MySqlGeometry>
Column type The MariaDB spatial types (GEOMETRY, POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON and GEOMETRYCOLLECTION)
Property type MySqlGeometry

Conversions

Method Direction Description
Get Database to property Converts the spatial value returned by the driver into a MySqlGeometry. A MySqlGeometry is returned as is, a byte[] is converted via new MySqlGeometry(MySqlDbType.Geometry, bytes), and any other value, including null and DBNull, is returned as a null geometry (IsNull is true).
Set Property to database Converts the MySqlGeometry into the value written into the spatial column, which the driver serializes as its MySQL/MariaDB internal format. A null geometry (IsNull is true) is written as null.

Usability

Bind the handler to the property that maps to a spatial column.

Attribute

public class Place
{
    public int Id { get; set; }

    [Map("location"), PropertyHandler(typeof(GeometryToMySqlGeometryPropertyHandler))]
    public MySqlGeometry Location { get; set; }
}

Once bound, the library invokes the handler automatically when the property is read or written.

using (var connection = new MariaDbConnection(connectionString))
{
    MySqlGeometry location = GetLocation(); // a MySqlGeometry created via the API of the driver
    connection.Insert(new Place { Id = 1, Location = location });
    var place = connection.Query<Place>(e => e.Id == 1).First();
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Place>()
    .PropertyHandler<GeometryToMySqlGeometryPropertyHandler>(e => e.Location);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Place, GeometryToMySqlGeometryPropertyHandler>(e => e.Location, new GeometryToMySqlGeometryPropertyHandler(), true);

Type Level Mapping

To apply the handler to every property of the type MySqlGeometry:

PropertyHandlerMapper.Add<MySqlGeometry, GeometryToMySqlGeometryPropertyHandler>(new GeometryToMySqlGeometryPropertyHandler(), true);