Idea: Can you initializes collections or give them lazy loading?

Aug 18, 2010 at 5:53 AM
Edited Aug 18, 2010 at 6:15 AM

Can you initializes collections or give them lazy loading?

 

e.g.

I have an Order, which has one Addresses, and in that it has 0-n Address, the generated result has my Order class with:

private List<Address> addressesField;
public List<Address> Addresses {
			get {
				return this.addressesField;
			}
			set {
				this.addressesField = value;
			}
		}

 

 

However, the field is not initialized, (i.e. the field is currently null)

 

Is it possible for the code generator to initialize all these fields? (Or to use the lazy "loading")

 

//e.g.
private List<Address> addressesField = new List<Address>();
//or
public List<Address> Addresses {
			get {
				if (addressesField == null) addressesField = new List<Address>();
				return this.addressesField;
			}
			set {
				this.addressesField = value;
			}
		}

 

Coordinator
Aug 18, 2010 at 7:19 AM

Of course you can,

Just set EnableInitializeFields to true and set EnableLazyLoading = true.

The result look like this :

        public VisiteInclusion VisiteInclusion {
            get {
                if ((this.visiteInclusionField == null)) {
                    this.visiteInclusionField = new VisiteInclusion();
                }
                return this.visiteInclusionField;
            }
            set {
                if ((this.visiteInclusionField != null)) {
                    if ((visiteInclusionField.Equals(value) != true)) {
                        this.visiteInclusionField = value;
                        this.OnPropertyChanged("VisiteInclusion", value);
                    }
                }
                else {
                    this.visiteInclusionField = value;
                    this.OnPropertyChanged("VisiteInclusion", value);
                }
            }
        }

Pascal.

Aug 18, 2010 at 7:25 AM
Edited Aug 18, 2010 at 7:47 AM

I've already set lazy loading to true, but it doesn't generate the lazy loading?

 

(I'm targeting a .net 2.0 frame work if that helps? Although I don't think it changes your code,  - e.g. If i try to implement change notification, it will try and use collection changed, even thou that is a 3.5 feature-)

 

I will experiment more with this and try to post up a sample of the xsd.

 

Vs 2010 Pro,
xsd2code 3.3 beta

 

 

 

 

 

 

 

Ok here is an example:

 

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:attribute name="name" type="xs:string" use="required"/>
      <xs:attribute name="street" type="xs:string" use="required"/>
      <xs:attribute name="zip" type="xs:string" use="required"/>
      <xs:attribute name="city" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Addresses">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Address" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Client">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Addresses" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="clientName" type="xs:string" use="optional"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

// ------------------------------------------------------------------------------
//  <auto-generated>
//    Generated by Xsd2Code. Version 3.3.0.29707
I'm manually adding link breaks to the next line, as it's stretchign the forum
// <NameSpace>SVC_Delta</NameSpace>
<Collection>List</Collection>
<codeType>CSharp</codeType>
<EnableDataBinding>False</EnableDataBinding>
<EnableLasyLoading>True</EnableLasyLoading>
<TrackingChangesEnable>False</TrackingChangesEnable>
<GenTrackingClasses>False</GenTrackingClasses>
<HidePrivateFieldInIDE>False</HidePrivateFieldInIDE>
<EnableSummaryComment>False</EnableSummaryComment>
<VirtualProp>False</VirtualProp>
<IncludeSerializeMethod>False</IncludeSerializeMethod>
<UseBaseClass>False</UseBaseClass>
<GenBaseClass>False</GenBaseClass>
<GenerateCloneMethod>False</GenerateCloneMethod>
<GenerateDataContracts>False</GenerateDataContracts>
<CodeBaseTag>Net20</CodeBaseTag>
<SerializeMethodName>Serialize</SerializeMethodName>
<DeserializeMethodName>Deserialize</DeserializeMethodName>
<SaveToFileMethodName>SaveToFile</SaveToFileMethodName>
<LoadFromFileMethodName>LoadFromFile</LoadFromFileMethodName>
<GenerateXMLAttributes>False</GenerateXMLAttributes>
<AutomaticProperties>False</AutomaticProperties>
<GenerateShouldSerialize>False</GenerateShouldSerialize>
<DisableDebug>False</DisableDebug>
<CustomUsings></CustomUsings>
<ExcludeIncludedTypes>False</ExcludeIncludedTypes>
<EnableInitializeFields>True</EnableInitializeFields>
// </auto-generated> // ------------------------------------------------------------------------------ namespace SVC_Delta { using System; using System.Diagnostics; using System.Xml.Serialization; using System.Collections; using System.Xml.Schema; using System.ComponentModel; using System.Collections.Generic; [System.Diagnostics.DebuggerStepThroughAttribute()] public partial class Address { private string nameField; private string streetField; private string zipField; private string cityField; public string name { get { return this.nameField; } set { this.nameField = value; } } public string street { get { return this.streetField; } set { this.streetField = value; } } public string zip { get { return this.zipField; } set { this.zipField = value; } } public string city { get { return this.cityField; } set { this.cityField = value; } } } [System.Diagnostics.DebuggerStepThroughAttribute()] public partial class Addresses { private List<Address> addressField; public List<Address> Address { get { return this.addressField; } set { this.addressField = value; } } } [System.Diagnostics.DebuggerStepThroughAttribute()] public partial class Client { private List<Address> addressesField; private string clientNameField; [System.Xml.Serialization.XmlArrayItemAttribute("Address", IsNullable=false)] public List<Address> Addresses { get { return this.addressesField; } set { this.addressesField = value; } } public string clientName { get { return this.clientNameField; } set { this.clientNameField = value; } } } }

 

 

As you can see, enableinitializers is set to true, but it never initializes collections

Coordinator
Aug 18, 2010 at 8:21 AM

Thank you for your feedback.

It was a bug caused by a recent modicication.

I published a new setup here : http://xsd2code.codeplex.com/releases/view/39563

Pascal.

 

Sep 3, 2010 at 8:06 AM

Is it possble to set flag @EnableLasyLoading to true by parameters. I can't do that.

Is there are exists alternative way to set up it?