-
Notifications
You must be signed in to change notification settings - Fork 20
Constructor
moh-hassan edited this page Mar 16, 2024
·
1 revision
The default generation of c# class or record are parameterless constructor.
A public Parameterized Constructor can be generated by the option -R <option>
OR --ctor <option>
.
Allowed values are:
-
full
to generate Parameters constructor with initializing proerties. -
none
(default value): No generation.
The parameters of the constructor are only:
- The not null properties.
- The non navigation properties.
Example
o2pgen -r https://services.odata.org/TripPinRESTierService --ctor full
OR using short option:
o2pgen -r https://services.odata.org/TripPinRESTierService -R full
Sample of the generated c# code:
// EntitySetName: PlanItem
public partial class PlanItem
{
public PlanItem (int planItemId, DateTimeOffset startsAt, DateTimeOffset endsAt, TimeSpan duration)
{
PlanItemId = planItemId;
StartsAt = startsAt;
EndsAt = endsAt;
Duration = duration;
}
public virtual int PlanItemId {get;set;} //PrimaryKey not null
public virtual string ConfirmationCode {get;set;}
public virtual DateTimeOffset StartsAt {get;set;} // not null
public virtual DateTimeOffset EndsAt {get;set;} // not null
public virtual TimeSpan Duration {get;set;} // not null
}
Click to show complete generated code
//o2pgen -r https://services.odata.org/TripPinRESTierService --ctor full -n
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Spatial;
namespace Trippin
{
// EntitySetName: People
public partial class Person
{
public Person (string userName, string firstName, PersonGender gender, Feature favoriteFeature)
{
UserName = userName;
FirstName = firstName;
Gender = gender;
FavoriteFeature = favoriteFeature;
}
public virtual string UserName {get;set;} //PrimaryKey not null
public virtual string FirstName {get;set;} // not null
public virtual string LastName {get;set;}
public virtual string MiddleName {get;set;}
public virtual PersonGender Gender {get;set;} // not null
public virtual long Age {get;set;}
public virtual List<string> Emails {get;set;}
public virtual List<Location> AddressInfo {get;set;}
public virtual Location HomeAddress {get;set;}
public virtual Feature FavoriteFeature {get;set;} // not null
public virtual List<Feature> Features {get;set;}
public virtual List<Person> Friends {get;set;}
public virtual Person BestFriend {get;set;}
public virtual List<Trip> Trips {get;set;}
}
// EntitySetName: Airlines
public partial class Airline
{
public Airline (string airlineCode)
{
AirlineCode = airlineCode;
}
public virtual string AirlineCode {get;set;} //PrimaryKey not null
public virtual string Name {get;set;}
}
// EntitySetName: Airports
public partial class Airport
{
public Airport (string icaoCode)
{
IcaoCode = icaoCode;
}
public virtual string Name {get;set;}
public virtual string IcaoCode {get;set;} //PrimaryKey not null
public virtual string IataCode {get;set;}
public virtual AirportLocation Location {get;set;}
}
// Complex Entity
public partial class Location
{
public virtual string Address {get;set;}
public virtual City City {get;set;}
}
// Complex Entity
public partial class City
{
public virtual string Name {get;set;}
public virtual string CountryRegion {get;set;}
public virtual string Region {get;set;}
}
// Complex Entity
public partial class AirportLocation : Location
{
public virtual GeographyPoint Loc {get;set;}
}
// Complex Entity
public partial class EventLocation : Location
{
public virtual string BuildingInfo {get;set;}
}
// EntitySetName: Trip
public partial class Trip
{
public Trip (int tripId, Guid shareId, float budget, DateTimeOffset startsAt, D
ateTimeOffset endsAt)
{
TripId = tripId;
ShareId = shareId;
Budget = budget;
StartsAt = startsAt;
EndsAt = endsAt;
}
public virtual int TripId {get;set;} //PrimaryKey not null
public virtual Guid ShareId {get;set;} // not null
public virtual string Name {get;set;}
public virtual float Budget {get;set;} // not null
public virtual string Description {get;set;}
public virtual List<string> Tags {get;set;}
public virtual DateTimeOffset StartsAt {get;set;} // not null
public virtual DateTimeOffset EndsAt {get;set;} // not null
public virtual List<PlanItem> PlanItems {get;set;}
}
// EntitySetName: PlanItem
public partial class PlanItem
{
public PlanItem (int planItemId, DateTimeOffset startsAt, DateTimeOffset endsAt
, TimeSpan duration)
{
PlanItemId = planItemId;
StartsAt = startsAt;
EndsAt = endsAt;
Duration = duration;
}
public virtual int PlanItemId {get;set;} //PrimaryKey not null
public virtual string ConfirmationCode {get;set;}
public virtual DateTimeOffset StartsAt {get;set;} // not null
public virtual DateTimeOffset EndsAt {get;set;} // not null
public virtual TimeSpan Duration {get;set;} // not null
}
// EntitySetName: Event
public partial class Event : PlanItem
{
public virtual EventLocation OccursAt {get;set;}
public virtual string Description {get;set;}
}
// EntitySetName: PublicTransportation
public partial class PublicTransportation : PlanItem
{
public virtual string SeatNumber {get;set;}
}
// EntitySetName: Flight
public partial class Flight : PublicTransportation
{
public virtual string FlightNumber {get;set;}
public virtual Airline Airline {get;set;}
public virtual Airport From {get;set;}
public virtual Airport To {get;set;}
}
// EntitySetName: Employee
public partial class Employee : Person
{
public Employee (long cost)
{
Cost = cost;
}
public virtual long Cost {get;set;} // not null
public virtual List<Person> Peers {get;set;}
}
// EntitySetName: Manager
public partial class Manager : Person
{
public Manager (long budget)
{
Budget = budget;
}
public virtual long Budget {get;set;} // not null
public virtual Location BossOffice {get;set;}
public virtual List<Person> DirectReports {get;set;}
}
public enum PersonGender
{
Male=0,
Female=1,
Unknown=2
}
public enum Feature
{
Feature1=0,
Feature2=1,
Feature3=2,
Feature4=3
}
}
The default generation of class or record is public
, but it can be internal
using the option --interal
Example
o2pgen -r https://services.odata.org/TripPinRESTierService --internal
Sample of the generated c# code:
// EntitySetName: PlanItem
internal partial class PlanItem
{
public virtual int PlanItemId {get;set;} //PrimaryKey not null
public virtual string ConfirmationCode {get;set;}
public virtual DateTimeOffset StartsAt {get;set;} // not null
public virtual DateTimeOffset EndsAt {get;set;} // not null
public virtual TimeSpan Duration {get;set;} // not null
}
- home
- Announcing V6.0.0
- Features
- Getting started with c# generation
- Http Connection
- Using Parameter file
- User Defined Attributes
- Controlling c# code generation
- Model Filter
- Enable Nullable Reference type of c# 8
- Class with Init-Only Properties (c# 9)
- Generating Constructor
- Record-Type (c# 9)
- Name Map
- Securing Password
- Using Proxy Server
- Using Plugin Attributes
- Developing with OData2Poco
- Examples in dotnetfiddle.net
- CommandLine-Reference
- AttributeExamples
- typescript generation
- Help Screen
- How to
- New Feature 4.2.1
Samples of generated code: