Skip to content

init only

moh-hassan edited this page Sep 11, 2022 · 2 revisions

Immutable classes with init-only property is a new feature in c# 9.

Class with init-only properties is declared as:


	public partial class Location
	{
	    public string Address {get;init;} 
	    public City City {get;init;} 
	}

Init-only properties can be generated using an option -I (upper case) or --init-only

Example

o2pgen -r https://services.odata.org/TripPinRESTierService -I -v

Or

o2pgen -r https://services.odata.org/TripPinRESTierService --init-only

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 class City
	{
	    public string CountryRegion {get;init;} // not null

	    public string Name {get;init;} // not null

	    public string Region {get;init;} // not null

	}

	public partial class Location
	{
	    public string Address {get;init;} // not null

	    public City City {get;init;} 

	}

	public partial class EventLocation : Location
	{
	    public string BuildingInfo {get;init;} 

	}

	public partial class AirportLocation : Location
	{
	    public GeographyPoint Loc {get;init;} // not null

	}

	public partial class Photo
	{
	    public long Id {get;} //PrimaryKey not null ReadOnly

	    public string Name {get;init;} 

	}

	public partial class 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 class Airline
	{
	    public string AirlineCode {get;} //PrimaryKey not null ReadOnly

	    public string Name {get;init;} // not null

	}

	public partial class 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 class 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 class PublicTransportation : PlanItem
	{
	    public string SeatNumber {get;init;} 

	}

	public partial class Flight : PublicTransportation
	{
	    public string FlightNumber {get;init;} // not null

	}

	public partial class Event : PlanItem
	{
	    public string Description {get;init;} 

	    public EventLocation OccursAt {get;init;} 

	}

	public partial class 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

Clone this wiki locally