Skip to main content
Version: Preview (0.3.0-preview.205)

Class ValueObjectTypeHandler<TSelf, TValue>

Namespace: AdCodicem.ValueObjects.Dapper
Assembly: AdCodicem.ValueObjects.Dapper.dll

Maps a value object to and from its underlying column value for Dapper.

public sealed class ValueObjectTypeHandler<TSelf, TValue> : SqlMapper.TypeHandler<TSelf>, SqlMapper.ITypeHandler where TSelf : struct, IValueObject<TSelf, TValue>

Type Parameters​

TSelf

Value object type.

TValue

Underlying value type.

Inheritance​

object ← SqlMapper.TypeHandler<TSelf> ← ValueObjectTypeHandler<TSelf, TValue>

Implements​

SqlMapper.ITypeHandler

Inherited Members​

SqlMapper.TypeHandler<TSelf>.SetValue(IDbDataParameter, TSelf), SqlMapper.TypeHandler<TSelf>.Parse(object), object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

Reading uses the trusted factory, on the same reasoning as the Entity Framework Core converter: the rows come from a database this application wrote through the validating factory, and read paths are hot. A column holding text where the underlying type is not text, or the reverse, is the exception: the value object did not write it. Text read into a value object whose underlying type is not is parsed, and so validated, the way the value object parses text. Anything else read into a value object over - a number from a numeric column, a from a uuid or uniqueidentifier one - is converted to text, then validated through TryCreate.

A parameter declares the column a value object over is mapped to when the value object says more than its underlying type, as the Entity Framework Core conventions map it: an entity identifier as fixed-length, non-Unicode text of its exact length, a value object declaring a maximum length as Unicode text of that length. SQL Server then compares the parameter with the column as it is, with no implicit conversion of the column to cost an index seek. The size is never smaller than the value, which both SqlClient and Npgsql would otherwise truncate.

A SQL NULL reads as null into an optional value object, TSelf?, and is refused with a for a required one in a single-column query, as Dapper refuses it for an . The handler implements itself for that, since cannot return null for a struct.

For a member of a mapped type, or a parameter of the constructor it is mapped through, Dapper checks for a NULL before it calls the handler, and never calls it: a required value object is left uninitialized, default(TSelf), and nothing throws. A column that can be NULL belongs in a TSelf? member.

Methods​

Parse(object)​

Parse a database value back to a typed value

public override TSelf Parse(object value)

Parameters​

value object

The value from the database

Returns​

TSelf

The typed value

Exceptions​

DataException

value is a SQL NULL, which a value object cannot hold, a value that cannot be converted to TValue, or text, or a value converted to text, that the value object refuses.

SetValue(IDbDataParameter, TSelf)​

Assign the value of a parameter before a command executes

public override void SetValue(IDbDataParameter parameter, TSelf value)

Parameters​

parameter IDbDataParameter

The parameter to configure

value TSelf

Parameter value