StringToGuidPropertyHandler

A property handler that maps a text-based UUID column, such as the MariaDB UUID type, into a Guid property.

A property handler that maps a text-based UUID column (for example, the MariaDB UUID type) into a Guid property.

Use StringToNullableGuidPropertyHandler for a Guid? property.

Overview

Name Description
Class StringToGuidPropertyHandler
Namespace RepoDb.PropertyHandlers
Package RepoDb
Implements IPropertyHandler<object, Guid>
Column type A text-based UUID column (for example, the MariaDB UUID type)
Property type Guid

Conversions

Method Direction Description
Get Database to property Converts the value returned by the driver (a Guid, or a string in the canonical 36-character form) into a Guid. null, DBNull and an empty string are returned as Guid.Empty.
Set Property to database Converts the Guid into its canonical 36-character text (for example 6f9619ff-8b86-d011-b42d-00c04fc964ff).

Exceptions

Exception Thrown When
ArgumentException The type of the value returned by the driver is not supported.
FormatException The text returned by the driver is not a valid UUID.

Usability

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

Attribute

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

    [Map("token"), PropertyHandler(typeof(StringToGuidPropertyHandler))]
    public Guid Token { get; set; }
}

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

using (var connection = new MariaDbConnection(connectionString))
{
    var token = Guid.NewGuid();
    connection.Insert(new Session { Id = 1, Token = token });
    var session = connection.Query<Session>(e => e.Id == 1).First();
    // session.Token equals token
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Session>()
    .PropertyHandler<StringToGuidPropertyHandler>(e => e.Token);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Session, StringToGuidPropertyHandler>(e => e.Token, new StringToGuidPropertyHandler(), true);

Type Level Mapping

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

PropertyHandlerMapper.Add<Guid, StringToGuidPropertyHandler>(new StringToGuidPropertyHandler(), true);

A type level mapping is process-wide: it is applied to every Guid property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless every Guid property maps to a text-based UUID column.

See Also