StringToIPAddressPropertyHandler

A property handler that maps a text-based network address column, such as the MariaDB INET4 and INET6 types, into an IPAddress property.

A property handler that maps a text-based network address column (for example, the MariaDB INET4 and INET6 types) into an IPAddress property.

Overview

Name Description
Class StringToIPAddressPropertyHandler
Namespace RepoDb.PropertyHandlers
Package RepoDb
Implements IPropertyHandler<object, IPAddress>
Column type A text-based network address column (for example, the MariaDB INET4 and INET6 types)
Property type IPAddress

Conversions

Method Direction Description
Get Database to property Converts the value returned by the driver (an IPAddress, a string, or a byte[]) into an IPAddress. null, DBNull, an empty string and an empty array are returned as null.
Set Property to database Converts the IPAddress into its text form (for example 192.168.0.1 or ::1). 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 IP address.

Usability

Bind the handler to the property that maps to an INET4 or INET6 column.

Attribute

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

    [Map("address"), PropertyHandler(typeof(StringToIPAddressPropertyHandler))]
    public IPAddress Address { 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 Host { Id = 1, Address = IPAddress.Parse("192.168.0.1") });
    var host = connection.Query<Host>(e => e.Id == 1).First();
    // host.Address is an IPAddress
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Host>()
    .PropertyHandler<StringToIPAddressPropertyHandler>(e => e.Address);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Host, StringToIPAddressPropertyHandler>(e => e.Address, new StringToIPAddressPropertyHandler(), true);

Type Level Mapping

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

PropertyHandlerMapper.Add<IPAddress, StringToIPAddressPropertyHandler>(new StringToIPAddressPropertyHandler(), true);