Skip to content

Cherry picking methods

fonlow edited this page Apr 5, 2024 · 2 revisions

While by default POCO2TS will do cherry picking according to DataContractAttribute and DataMemberAttribute, it supports other options of development processes.

When you run the program without any argument, you will get a simple help screen.

Poco2Ts.exe generates TypeScript data model interfaces from POCO classes.
Example:
For classes decorated by DataContractAttribute:
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts
For classes decorated by Newtonsoft.Json.JsonObjectAttribute:
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts /2
For classes decorated by SerializableAttribute:
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts /4
For public classes, properties and properties,  and use System.ComponentModel.DataAnnotations.RequiredAttribute:
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts /8
For cherry picking with .NET Core JsonPropertyNameAttribute:
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts /16
For all classes, properties and fields
  Fonlow.Poco2Ts.exe MyAssemblyWithPOCO.dll MyOutputTS.ts /0

And the options could be described by this enum type:

    /// <summary>
    /// Flagged options for cherry picking in various development processes.
    /// </summary>
    [Flags]
    public enum CherryPickingMethods
    {
        /// <summary>
        /// Include all public classes, properties and properties.
        /// </summary>
        All = 0,

        /// <summary>
        /// Include all public classes decorated by DataContractAttribute, and public properties or fields decorated by DataMemberAttribute. 
        /// And use DataMemberAttribute.IsRequired
        /// </summary>
        DataContract =1,

        /// <summary>
        /// Include all public classes decorated by JsonObjectAttribute, and public properties or fields decorated by JsonPropertyAttribute.  
        /// And use JsonPropertyAttribute.Required
        /// </summary>
        NewtonsoftJson = 2,

        /// <summary>
        /// Include all public classes decorated by SerializableAttribute, and all public properties or fields but excluding those decorated by NonSerializedAttribute.
        /// And use System.ComponentModel.DataAnnotations.RequiredAttribute.
        /// </summary>
        Serializable = 4,

        /// <summary>
        /// Include all public classes, properties and properties. 
        /// And use System.ComponentModel.DataAnnotations.RequiredAttribute.
        /// </summary>
        AspNet = 8,

        /// <summary>
        /// For .NET Core Json*Attribute
        /// </summary>
        NetCore= 16,
    }

DataContract is the default option in POCO2TS.

So if you somehow have some classes decorated by DataContractAttribute and others by JsonObjectAttribute, and you want to include both sets, then the option is /3. And DataMemberAttribute and JsonPropertyAttribute will be handled accordingly.

Remarks

  • While POCO2TS.exe can handle Newtonsoft.Json attributes, it does not have static binding with Newtonsoft.Json. It is your data model assembly has binding with Newtonsoft.Json.dll. When you use the NewtonsoftJson option, .NET runtime will try to locate it. If the runtime fails to locate it, POCO2TS will resolve through checking the folder where the data model assembly is located. This resolution is also useful when your data model assembly has dependency on the other data model assembly.
  • With CherryPickingMethod NetCore, JsonPropertyNameAttribute, JsonIgnoreAttribute and JsonRequiredAttribute of System.Text.Json.Serialization are used. However, according to Migrate from Newtonsoft.Json to System.Text.Json, these properties are only for JSON serialization, while Newtonsoft.Json and System.Runtime.Serialization takes "Design by Contract" in design, therefor, Newtonsoft.Json provides JsonObjectAttribute and System.Runtime.Serialization provides DataContractAttribute to decorate a type/class.
  • Without something similar to DataContractAttribute, with cherryPickingMethod.NetCore, you may just put all types to be exposed to clients into an assembly. Good for decoupling / "separation of concerns" / service loading anyway.
Clone this wiki locally