Problem with lazy instantiation

Nov 3, 2009 at 4:07 PM

Lazy instanciation cause many regression bugs. Is it possible to create a parameter to disable it.

For example, if I have a code that test if a property is NULL now it always return false because of lazy instanciation.

An other wish : could you add a parameter to add a [Browsable(false)] attribute on generated properties ?

Thanks for your help.

 

Coordinator
Nov 4, 2009 at 8:09 AM
Hi,
Thank you for you feedback.
It's strange because in previous release, instancation were done in ctor.
So the properties were never null.
Can you give me an exemple (code and xsd).

however, i'am agree with you, Lazy instanciation can be enable or disable.
I'll work on this functionality soon.
I am French, so excuse me for the quality of translation :)
Bye.

2009/11/3 syl74 <notifications@codeplex.com>

From: syl74

Lazy instanciation cause many regression bugs. Is it possible to create a parameter to disable it.

For example, if I have a code that test if a property is NULL now it always return false because of lazy instanciation.

An other wish : could you add a parameter to add a [Browsable(false)] attribute on generated properties ?

Thanks for your help.

 

Read the full discussion online.

To add a post to this discussion, reply to this email (Xsd2Code@discussions.codeplex.com)

To start a new discussion for this project, email Xsd2Code@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Nov 4, 2009 at 10:13 AM

Thank you for your answer.

In fact, un the previous version, I set some properties to NULL explicitly. Then when I re-read the property again, it is not null due to "lazyloading" => that's why I am encountring the problem.

If you could give us a way to add a [Browsable(false)] attribute, It will be great. We'd like to have the functionality because, you create a (for example) "name" property. And we would like to bind on a "Name" property (Pascal Case). So we add a "Name" property in a partial case but if "name" AND "Name" exists on the class, DataBinding in Winforms is NOT case sensitive so we cannot guaranty that we will bind on the "right" property. If we could add [Browsable(false)] on "name" than we are sure that binding will be done on "Name" property as we wish.

Thanks for your help.

Coordinator
Nov 4, 2009 at 2:20 PM
Ok about lazyloading.
For Browsable attribute, i need to make an important evolution of xsd2code.
The current default is that it will be applied to all properties.

The next version of xsd2Code will have a global setting and a custom properties setting.
In this case it will be possible to add the Browsable attribute for all or just for one property.
So you will have to wait for next release. (I make a major release every two or three months).

However you can change the code to add Browsable attribute according to your own needs.
You can do it your self in CodeExtention.cs (ProcessProperty method in assembly Xsd2Code.Library).
Something like this :
if (GeneratorContext.GeneratorParams.GenerateBrowsableAttribute)
{
var attrib = new CodeTypeReference("System.ComponentModel.BrowsableAttribute");
prop.CustomAttributes.Add(new CodeAttributeDeclaration(attrib));
}
Bye.
Pascal.

2009/11/4 syl74 <notifications@codeplex.com>

From: syl74

Thank you for your answer.

In fact, un the previous version, I set some properties to NULL explicitly. Then when I re-read the property again, it is not null due to "lazyloading" => that's why I am encountring the problem.

If you could give us a way to add a [Browsable(false)] attribute, It will be great. We'd like to have the functionality because, you create a (for example) "name" property. And we would like to bind on a "Name" property (Pascal Case). So we add a "Name" property in a partial case but if "name" AND "Name" exists on the class, DataBinding in Winforms is NOT case sensitive so we cannot guaranty that we will bind on the "right" property. If we could add [Browsable(false)] on "name" than we are sure that binding will be done on "Name" property as we wish.

Thanks for your help.

Read the full discussion online.

To add a post to this discussion, reply to this email (Xsd2Code@discussions.codeplex.com)

To start a new discussion for this project, email Xsd2Code@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Nov 4, 2009 at 3:29 PM
Edited Nov 4, 2009 at 3:35 PM

Don't forget, that with lazy instantiation, when serializing the XmlSerializer is accessing the properties so XmlSeralizer will cause everything to instantiate...

 

i.e. right now, lazy instantiation is of no benefit for time/processing saving when serializing, because XmlSerializer is going to access everything that is not [XmlIgnore] and instantiate all the properties anyway..

 

Suggest you make a class-wide boolean variable/property _serializing = false; and set it to true when Serialize() is called.. Then your lazy code can look like:

get {

  if(!_serializing && theField == null)
    theField = new TheField();
  return theField;

}

 

-

 As to how this causes bugs, the serialized XML differs from before. Suppose we have a dumb service that relies on the presence/absence of an element TheField. Suppose we have a class:

class{
  property A: int
  property TheField: TheFieldClass
}

With NOT LAZY instantiation, if TheField is null we get:

<root>
  <A>1</A>
</root>

Now with lazy, we get:

<root>
  <A>1</A>
  <TheField />
</root>

 

Now suppose TheFieldClass has an int property B:

<root>
  <A>1</A>
  <TheField>
    <B>0</B>
  </TheField>
</root>

 

Even though I as a programmer never touched the property, just serializing it caused it to exist in the xml and now have values in primitive fields which might cause services that rely on present/absent tags, to fail..

Coordinator
Nov 4, 2009 at 7:27 PM
Yes that's right, the serialization will trigger the instantiation.
But the classes generated by xsd2code are not only used for serialization.
In my development I use the generated classes for databinding in wpf or silverlight application.
In this case lazy instantiation is interesting.
I'll make the azy instantiation customizable so that each can decide to use it or not.


2009/11/4 cjard <notifications@codeplex.com>

From: cjard

Don't forget, that with lazy instantiation, when serializing the XmlSerializer is accessing the properties so XmlSeralizer will cause everything to instantiate...

 

i.e. right now, lazy instantiation is of no benefit for time/processing saving when serializing, because XmlSerializer is going to access everything that is not [XmlIgnore] and instantiate all the properties anyway..

 

Suggest you make a class-wide boolean variable/property _serializing = false; and set it to true when Serialize() is called.. Then your lazy code can look like:

get {

  if(!_serializing && theField == null)
    theField = new TheField();
  return theField;

}

 

-

 

Read the full discussion online.

To add a post to this discussion, reply to this email (Xsd2Code@discussions.codeplex.com)

To start a new discussion for this project, email Xsd2Code@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com