StringToNullableGuidPropertyHandler

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

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

Overview

Name Description
Class StringToNullableGuidPropertyHandler
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 null.
Set Property to database Converts the Guid into its canonical 36-character text (for example 6f9619ff-8b86-d011-b42d-00c04fc964ff). A null value is written as null.

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 nullable UUID column.

Attribute

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

    [Map("token"), PropertyHandler(typeof(StringToNullableGuidPropertyHandler))]
    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))
{
    connection.Insert(new Session { Id = 1, Token = null });
    var session = connection.Query<Session>(e => e.Id == 1).First();
    // session.Token is null
}

Fluent Mapping

To configure via FluentMapper:

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

Property Level Mapping

To configure via PropertyHandlerMapper:

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

Type Level Mapping

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

PropertyHandlerMapper.Add<Guid?, StringToNullableGuidPropertyHandler>(new StringToNullableGuidPropertyHandler(), 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