MySqlObjectToGeometryPropertyHandler

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

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

Overview

Name Description
Class MySqlObjectToGeometryPropertyHandler
Namespace RepoDb.PropertyHandlers.MySql
Package RepoDb.MySql
Implements IPropertyHandler<object, MySqlGeometry>
Column type The MySQL 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 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(MySqlObjectToGeometryPropertyHandler))]
    public MySqlGeometry Location { get; set; }
}

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

using (var connection = new MySqlConnection(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<MySqlObjectToGeometryPropertyHandler>(e => e.Location);

Property Level Mapping

To configure via PropertyHandlerMapper:

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

Type Level Mapping

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

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