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 MySqlConnector based connector of the package.

Overview

Name Description
Class GeometryToMySqlGeometryPropertyHandler
Namespace RepoDb.PropertyHandlers.MariaDbConnector
Package RepoDb.MariaDbConnector
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 MySqlGeometry.FromMySql(bytes), and any other value, including null and DBNull, is returned as null.
Set Property to database Passes the MySqlGeometry to the driver, which serializes it as its MySQL/MariaDB internal format.

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);