OData2Poco is a code generation tool for generating plain-old CLR objects (POCO/DTO) in c# and typescript from OData feeds. POCO classes can be used in a typed RESTful client OData services. Code generation can be controlled by setting many options.
Branch | Build status |
---|---|
Master |
- New Feature: Gzip encoded content #48 by @DerekGn. Allow reading SAP metadata compressed as gzip.
Code Generation
-
A new powerful option
--att-defs
allows you to dynamically generate attributes for c# classes and properties using a simple text file that contains your template with expressions. These expressions are valid C# code that can utilize C# string functions and other built-in extension methods. You can also filter on classes and properties to apply the attributes selectively. -
Add comments to the header of c# class to mark the openType classes or Entity Types: EntitySet or Complex type.
Security
- Password/secret token are encrypted when read from commandLine/file and it's stored in a SecuredContainer.
- Reading password from keyboard and Encrypted then stored in a SecuredContainer.
User Experience
- Load option and arguments of commandLine from a text configuration File.
- Reading value of any option in the commandLine from file.
- Support repeating options in the commandLine for sequence args.
Http Connection
- New Option: Allow setting of SSL/TLS protocols.
- New option: Allow to Skip Certification Check in http connection.
- New Option: Allow to specify Http header in Odata http connection with the computing of base64.
Enhancement
- Set exit codes to be positive number to match Linux standard.
- Centeralizing packages and update all packages to the last version including Newtonsoft.Json to 13.0.3
Try the new version and let me know your feedback.
The developed packages can be downloaded from myget.org
If you are using this project, please show your support by giving this project a star!. Thanks!
Features of OData2Poco
- Generate POCO classes corresponding to the Entities defined in the XML MetaData stored in OData Feeds.
- Generate c# code classes, classes with init-only properties of c#8 and records.
- Generate typescript code as a single file or multi files(modules).
- Generation is based on the Metadata of the service stored on the server/ EDMX xml files or xml string contents.
- Support http(s) with different methods of authentication. The Supported autherizations are: Basic,Bearer,token,Ntlm,Digest, Custom and Oauth2.
- Console CommandLine tool Support .NET472 or higher.
- Class library Support NET6/netstandard2.0/net461.
- Support Windows or Linux /OS fx (with mono installed) and NET6 (netcore).
- Packaged as a nuget package in three different packages:
- A Class library full framework/ netstandard2.0 /NET5 for programming.
- A console CommandLine tool (one executable file o2pgen.exe)
- Global net core support NET6 (dotnet-o2pgen).
- Console tool o2pgen is published as a Chocolatey package.
- Generating CSharp POCO classes. Other languages may be added in the near future based on the community needs.
- Convert Data type of EDMX to the corresponding CLR data types.
- Support Entites, complex data type, Collections and navigation properties.
- Support OData service version V1..V4
- Code generation is controlled by setting different options:
-
User defined Atributes for the classes and properties using simple text file with c# Expressions.
-
Built-in Attributes: - Add Key Attributes. - Add Required Attributes to the properties. - Add JsonProperty Attribute to the properties. - Add Table Attribute to the class. - Add DataMember Attribute to the properties and DataContract Attribute to the class. - Add display attribute to the properties. - Add ProtoMember to the properties and ProtoContract to the class to suport Proto Buffer. - Add user defined attribute for the properties.
-
Adding virtual modifier to the properties.
-
Convert name of properties to camelCase or PasCase
-
Add nullable datatypes, e.g. int?.
-
Generate (or not) navigation properties.
-
Generated class follows inheritance hierarchy of OData feed (unless switched-off).
-
Generated class can inherit from a common BaseClass/interface.
-
Define namespace to overwrite the namespace of the model.
-
Filter Entities.
-
Name Mapping of Entities and properties using json file with regex support.
-
- Add primary key/mandatory comments to the properties of the class.
- Rename class/properties that have a name match a c# reserved keyword.
- Save metadata and generated code to a user defined file name.
- Support colored console windows /linux /OS fx.
- Support Microsoft.OData.Edm library version 7.5+ (OData v4).
- Support Microsoft.Data.Edm library (OData v1-v3).
Features added in V3.2.0:
- New: Support Windows NTLM authentication(Thanks to @lobster2012-user for help).
- New: Support Microsoft Digest authentication.
- New: Support Integrated Windows Authentication, enabling users to log in with their Windows credentials (Issue#19).
- New: Add jsonProperty(originalName) to properties that are renamed because its name is the same as its enclosing type.
- New: Show/hide model warning due to renaming properties/classes whose name is a reserved keyword.
- New: Support abstract class.
- New: support complex type inheritance
- New: Add attribute [MaxLength] for max length of string/byte[] properties.
- Convert EDM.TIME in Odata v3 to TimeSpan.
- Support multi schema.
- Support multi containers in OData v3.
- New feature in v5.0.1
- New feature in v6.0.
OData2Poco is available in three flavers:
- A class library: support NET6/netstandard2.0/net461,download.
- A Console tool: OData2Poco.CommandLine support net472 (a.k.a o2pgen), download.
- A .Net Core Global tool
dotnet-o2pgen
support NET6, download. - Checolatey Console tool, download.
OData2Poco.CommandLine is a Console application (net472) named o2pgen.
It Can be installed from Nuget Gallery:
Install-Package OData2Poco.CommandLine
Add the next xml code to the project.csproj:
<Target Name="Odata2PocoRun" BeforeTargets="CoreCompile">
<PropertyGroup>
<EnableCodeGeneration>true</EnableCodeGeneration>
<o2pgen>$(PkgOData2Poco_CommandLine)\tools\o2pgen.exe</o2pgen>
<options>-r http://services.odata.org/V4/Northwind/Northwind.svc/ -f Model\north.cs</options>
</PropertyGroup>
<Message Text="Executing o2pgen.exe" Importance="High" />
<Exec Condition="$(EnableCodeGeneration)
Command="$(o2pgen) $(options)" />
</Target>
The attribute Options
is the commandLine arguments. Modify the commandline options as you want.
For more details read Getting Start.
In visual studio 2019 and higher, o2pgen can be run directly from PowerShell Console (PCM). Its path is set during installation.
Check application is installed:
PM> o2pgen --version
O2pgen cli can run on Linux and Mac/OS if Mono is installed.
Install from nuget gallary, run the command:
dotnet tool install --global OData2Poco.dotnet.o2pgen
Run the command:
dotnet o2pgen -r http://services.odata.org/V4/Northwind/Northwind.svc/
For help type: dotnet o2pgen --help
Review Commandline option.
You can auto run dotnet o2pgen
from within MsBuild Target and save code in the project folder.
Add the next Msbuild target to your project and modify command parameters as needed.
When the property EnableCodeGeneration
is set to false
, no code is generated.
The generated code is saved to file northwind.cs
in the folder Model in the root of the project.
<Target Name="GenerateCode" BeforeTargets="CoreCompile">
<PropertyGroup>
<EnableCodeGeneration>true</EnableCodeGeneration>
</PropertyGroup>
<Exec Condition="$(EnableCodeGeneration)"
Command="dotnet o2pgen -r http://services.odata.org/V4/Northwind/Northwind.svc/ -f $(MSBuildProjectDirectory)\Model\northwind.cs -B">
</Exec>
</Target>
Support NET6/netstandard2.0/net461. It can be installed from Nuget Gallery
Install-Package OData2Poco
Try demo Application in NET6 Online
From Chocolatey Gallery:
choco install odata2poco-commandline
Read the:Wiki
License
MIT License.