ArrayToListPropertyHandler

A property handler that maps a database array column, which the driver returns as an array, into a generic list property.

A property handler that maps a database array column (for example a PostgreSQL T[] column), which the driver returns as an array, into a List<T> property.

Overview

Name Description
Class ArrayToListPropertyHandler<T>
Namespace RepoDb.PropertyHandlers
Package RepoDb
Implements IPropertyHandler<T[], List<T>>
Column type A database array column that the driver returns as T[] (for example, the PostgreSQL text[] and integer[] types)
Property type List<T>

Conversions

Method Direction Description
Get Database to property Copies the array returned by the driver into a new List<T>. A null value is returned as null.
Set Property to database Converts the List<T> into an array via ToArray(). A null list is written as null.

Usability

Bind the handler, closed over the element type, to the property that maps to a text[] column.

Attribute

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

    [Map("tags"), PropertyHandler(typeof(ArrayToListPropertyHandler<string>))]
    public List<string> Tags { get; set; }
}

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

using (var connection = new NpgsqlConnection(connectionString))
{
    connection.Insert(new Article { Id = 1, Tags = new List<string> { "orm", "dotnet" } });
    var article = connection.Query<Article>(e => e.Id == 1).First();
    // article.Tags is a List<string> with the elements of the text[] column
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Article>()
    .PropertyHandler<ArrayToListPropertyHandler<string>>(e => e.Tags);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Article, ArrayToListPropertyHandler<string>>(e => e.Tags, new ArrayToListPropertyHandler<string>(), true);

Type Level Mapping

To apply the handler to every property of the type List<string>:

PropertyHandlerMapper.Add<List<string>, ArrayToListPropertyHandler<string>>(new ArrayToListPropertyHandler<string>(), true);

A type level mapping is process-wide: it is applied to every List<string> property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless every List<string> property maps to a database array column.

See Also