Nino handles C# records with different constructor strategies:
- Primary Constructors: By default, Nino prefers the primary constructor of a record.
- Overriding Primary Constructors: If a record has a primary constructor but you want Nino to use a different one, mark the desired constructor with
[NinoConstructor]. - Caveat: If you mark a parameterless constructor with
[NinoConstructor] on a record that has a primary constructor, Nino will use the parameterless one, effectively discarding the values that would have been passed to the primary constructor during deserialization.
// Case 1: Primary constructor is used automatically
[NinoType]
public record SimpleRecord4(int Id, string Name, DateTime CreateTime)
{
[NinoIgnore] public bool Flag;
public int ShouldNotIgnore;
public SimpleRecord4() : this(0, "", DateTime.MinValue) { }
}
// Case 2: Primary constructor is ignored because [NinoConstructor] is on the parameterless one
[NinoType]
public record SimpleRecord5(int Id, string Name, DateTime CreateTime)
{
[NinoConstructor]
public SimpleRecord5() : this(0, "", DateTime.MinValue) { }
}