OracleIntervalYMToTotalMonthsPropertyHandler

A property handler that maps the Oracle INTERVAL YEAR TO MONTH type into a total number of months.

A property handler that maps the Oracle INTERVAL YEAR TO MONTH type into a total number of months, as a long? property. A TimeSpan cannot be used for this type, as years and months are calendar units of variable length.

Overview

Name Description
Class OracleIntervalYMToTotalMonthsPropertyHandler
Namespace RepoDb.PropertyHandlers.Oracle
Package RepoDb.Oracle
Implements IPropertyHandler<object, long?>
Column type The Oracle INTERVAL YEAR TO MONTH type
Property type long?

Conversions

Method Direction Description
Get Database to property Converts the value returned by the driver (an OracleIntervalYM, or any value convertible to long) into the total number of months. null, DBNull and an OracleIntervalYM.Null value are returned as null.
Set Property to database Converts the total number of months into an OracleIntervalYM, to be written into the column. A null value is written as null.

Usability

Bind the handler to the property that maps to an INTERVAL YEAR TO MONTH column.

Attribute

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

    [Map("term"), PropertyHandler(typeof(OracleIntervalYMToTotalMonthsPropertyHandler))]
    public long? TermInMonths { get; set; }
}

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

using (var connection = new OracleConnection(connectionString))
{
    connection.Insert(new Contract { Id = 1, TermInMonths = 30 }); // 2 years and 6 months
    var contract = connection.Query<Contract>(e => e.Id == 1).First();
    // contract.TermInMonths is 30
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Contract>()
    .PropertyHandler<OracleIntervalYMToTotalMonthsPropertyHandler>(e => e.TermInMonths);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Contract, OracleIntervalYMToTotalMonthsPropertyHandler>(e => e.TermInMonths, new OracleIntervalYMToTotalMonthsPropertyHandler(), true);

Type Level Mapping

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

PropertyHandlerMapper.Add<long?, OracleIntervalYMToTotalMonthsPropertyHandler>(new OracleIntervalYMToTotalMonthsPropertyHandler(), true);

A type level mapping is process-wide: it is applied to every long? property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless every long? property maps to a INTERVAL YEAR TO MONTH column.

See Also