Dapper: C# 9 records

Created on 16 Nov 2020  路  4Comments  路  Source: StackExchange/Dapper

Are there any plans to add support for records (added in C# 9/.NET 5), more specifically positional records?

All 4 comments

well, we'd need to think about what that means, since positional records still have names; do we bind by constructor parameter name (which would be my expectation)? or by ordinal position? what would you expect to happen here?

By name sounds most logical to me. See here on how it can be done.

I'm very familiar; right now, for nominal matching, it needs a parameterless constructor, which means that (if we assume the columns aren't an exact match) this doesn't work:

``` c#
private record PositionalCarRecord(int Age, Car.TrapEnum Trap, string Name)

but this does:

``` c#
private record PositionalCarRecord(int Age, Car.TrapEnum Trap, string Name)
{
    public PositionalCarRecord() : this(default, default, default) { }
}

I'll have to look to see how much work is involved in handling the first case.

I did it by name in spanjson, fwiw it was more work to get mixed records (with extra normal properties) to work than the positional stuff.
Additionally https://stackoverflow.com/questions/63097273/c-sharp-9-0-records-reflection-and-generic-constraints might help, but Marc probably already knows about it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yozawiratama picture yozawiratama  路  5Comments

PeterWone picture PeterWone  路  5Comments

unipeg picture unipeg  路  3Comments

huobazi picture huobazi  路  4Comments

valinoment picture valinoment  路  4Comments