record type - moh-hassan/odata2poco GitHub Wiki
C# 9 introduces record types or just records, a new reference type that you can create instead of classes. You can generate record types with immutable properties that has init only properties.
The c# 9 record type is perfect for use as data transfer objects (DTO) where immutability is valuable. The record type is fully supported by ASP.NET core 5/6 during model binding.
Record is declared as:
public partial record Airline
{
public string AirlineCode {get;init}
public string Name {get;init;}
}
Record type can be generated using an option -R (upper case)
or --record
Also, to set properties immutable use
-I (upper case)
or --init-only
o2pgen -r https://services.odata.org/TripPinRESTierService -I -R -v
Or
o2pgen -r https://services.odata.org/TripPinRESTierService --record --init-only -v
Click to show generated code
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Spatial;
namespace Microsoft.OData.SampleService.Models.TripPin
{
public partial record City
{
public string CountryRegion {get;init;} // not null
public string Name {get;init;} // not null
public string Region {get;init;} // not null
}
public partial record Location
{
public string Address {get;init;} // not null
public City City {get;init;}
}
public partial record EventLocation : Location
{
public string BuildingInfo {get;init;}
}
public partial record AirportLocation : Location
{
public GeographyPoint Loc {get;init;} // not null
}
public partial record Photo
{
public long Id {get;} //PrimaryKey not null ReadOnly
public string Name {get;init;}
}
public partial record Person
{
public string UserName {get;} //PrimaryKey not null ReadOnly
public string FirstName {get;init;} // not null
public string LastName {get;init;} // not null
public List<string> Emails {get;init;}
public List<Location> AddressInfo {get;init;}
public PersonGender Gender {get;init;}
public long Concurrency {get;} // not null ReadOnly
}
public partial record Airline
{
public string AirlineCode {get;} //PrimaryKey not null ReadOnly
public string Name {get;init;} // not null
}
public partial record Airport
{
public string IcaoCode {get;} //PrimaryKey not null ReadOnly
public string Name {get;init;} // not null
public string IataCode {get;init;} // not null
public AirportLocation Location {get;init;}
}
public partial record PlanItem
{
public int PlanItemId {get;} //PrimaryKey not null ReadOnly
public string ConfirmationCode {get;init;}
public DateTimeOffset StartsAt {get;init;}
public DateTimeOffset EndsAt {get;init;}
public TimeSpan Duration {get;init;}
}
public partial record PublicTransportation : PlanItem
{
public string SeatNumber {get;init;}
}
public partial record Flight : PublicTransportation
{
public string FlightNumber {get;init;} // not null
}
public partial record Event : PlanItem
{
public string Description {get;init;}
public EventLocation OccursAt {get;init;}
}
public partial record Trip
{
public int TripId {get;} //PrimaryKey not null ReadOnly
public Guid ShareId {get;init;}
public string Description {get;init;}
public string Name {get;init;} // not null
public float Budget {get;init;} // not null
public DateTimeOffset StartsAt {get;init;} // not null
public DateTimeOffset EndsAt {get;init;} // not null
public List<string> Tags {get;init;}
}
public enum PersonGender
{
Male=0,
Female=1,
Unknown=2
}
}
Note: New feature in V5.0.0