Home / Blog / Documentation / Code Generator / Settings / Properties
Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. It is possible to activate this option in xsd2code++ to obtain a more compact code, which improves the readability of classes.
This parameter allows to instantiate objects only at the moment of their use. Here is an example of generated code :
Lazy initialization is a technique that defers the creation of an object until the first time it is needed. In other words, initialization of the object happens only on demand. Note that the terms lazy initialization and lazy instantiation mean the same thing—they can be used interchangeably. By taking advantage of lazy initialization, you can improve the application’s performance by avoiding unnecessary computation and memory consumption. In this article we’ll look at how we can perform lazy initialization in C# with xsd2code++.
To enable this feature, you must set it in the settings: PROPERTIES.LAZYLOADING (TRUE/FALSE)
public List<dvd> Dvds { get { if ((dvdsField == null)) { dvdsField = new List<dvd>(); } return dvdsField; } set { ... } }
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.
For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.
For change notification to occur in a binding between a bound client and a data source, xsd2code++ generates the code that :
- Implement the INotifyPropertyChanged interface.
- Provide a change event à each time the value is changed or set to null.
public partial class RootSchemaCustomer : INotifyPropertyChanged { #region Private fields private PurchaseOrderType _purchaseOrder; private static MessagePackSerializer _serializer; #endregion public RootSchemaCustomer() { _purchaseOrder = new PurchaseOrderType(); } public PurchaseOrderType PurchaseOrder { get { return _purchaseOrder; } set { if ((_purchaseOrder == value)) { return; } if (((_purchaseOrder == null) || (_purchaseOrder.Equals(value) != true))) { _purchaseOrder = value; OnPropertyChanged("PurchaseOrder"); } } }
PascalCase is a naming convention in which the first letter of each word in a compound word is capitalized. If you use this naming convention, you can activate it in your generated classes.
Xsd2Code++ takes care of injecting the necessary attributes to maintain compliance with the XML or JSON schema.
Within the naming convention that you wish to adopt, you have the possibility to define if the private elements begin or end with :
Specified Conditional Serialization Pattern allows to exclude properties during the serialization process.
This parameter is used to generate additional properties which are used to determine whether a property should be serialized or not. There are several possibilities:
None | This feature is disabled |
Specified |
Generates a property ([PropertyName]Specified) that allows to indicate if a property must be taken into account in the serialization. In the example below if IsValidSpecified is set to true, the IsValid property will be taken into the serialization process. Otherwise the property will be ignored. This option allows to define by programming which properties will be serialized or not.
public int PublishYear { get { return _publishYear; } set { _publishYear = value; } } [XmlIgnore()] public bool PublishYearSpecified { get { return _publishYearSpecified; } set { _publishYearSpecified = value; } } In the example below, the code shows that it is necessary for each of the properties to implicitly specify that the serializer must include the property : DvdCollection collectionDvd = new DvdCollection(); dvd newdvd = new dvd(); newdvd.PublishYear = 2000; newdvd.PublishYearSpecified = true; collectionDvd.Dvds.Add(newdvd); collectionDvd.DvdsSpecified = true; collectionDvd.SaveToFile("c:\temp\dvds.xml"); |
Virtual properties behave like virtual methods, except for the differences in declaration and invocation syntax.
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
public virtual PurchaseOrderType PurchaseOrder { ... }
public override PurchaseOrderType PurchaseOrder { ... }
While working with the international pioneer in the aerospace sector Airbus we were led to interacting with schemas that contained elements whose types could be chosen from a list.
This new setting "ExpandObjectItem" ensures theses situation are handled properly by the generator.
With this setting enabled, wherever you could find a choice between types, you will now find in your generated class distinct properties for every choice available, giving you the option to use the option you need.
In the example given above, our choice is to be made between "SupportDig" and "SupportPhy", being an xsd we are dealing with an xs:choice. Here's what it looks like in a C# class generated with the ExpandObjectItem setting enabled.
By default Xsd2Code++ can handle xs:choices without enabling this setting, when you're using an xsd with choices you have the option to use this setting. Here's what it look like by default, thanks to .Net it can be done by taking it through an item list with the help of the built in Xml tools.
The main reason why this setting was put in place is JSon, by default Xsd2code++ won't be able to generate appropriate code without this setting enabled
So if you have a choice to make in JSon you NEED to enable the "ExpandObjectItem" setting, here's what the JSon equivalent of an xs:choice looks like :