Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > Optional properties

Reply
Thread Tools

Optional properties

 
 
Alex Dinu
Guest
Posts: n/a
 
      12-20-2004
If you have a data class, Person, with two properties, FirstName and
LastName, and at a later stage you want to add a new optional property to the
class which is used in a web service. What is the best way to achieve this in
order to minimize the effect on the cosumers, who may be running .NET and
Java platforms.
 
Reply With Quote
 
 
 
 
Dan Rogers
Guest
Posts: n/a
 
      12-20-2004
Hi Alex,

This is possible, from a code perspective, but you need to realize that
this is a change in the contracted interface, so there are no guarantees
that adding such a change at a later time will have zero impact on an
existing caller. For instance, if some caller implementation took a copy
of the WSDL at the time the contract between caller/callee was established,
and uses the schemas in that WSDL to validate proper XML schema compliance,
if you then later add properties to the class, and these get serialized,
the validation check on the caller's part will fail - resulting in issues
you may not anticipate.

However, if you are among those folks that like to think of XML as a better
comma delimited file, and you just seek out the elements you are looking
for (say with xpath) or with an optimized pull parser (like that found in
NET), then you can get away with this in most circumstances.

The way to add more properties depends on their type. For reference types
in .NET, just create a normal property or field, and place it at the end of
your class. You need to take some care that you only serialize either thru
fields or thru properties, and never mix them, since this can cause changes
added at end to appear in the middle. This is due to the way the
serializer first processes fields and then properties in order (or is it
the other way around)...

For reference types, they are not serialized if their values are null at
runtime. For value types, such as bool and int, you have to do something
slightly different. Value types never have a value, since as soon as you
declare a value type, it is assigned a value as it is initialized. For
this reason, value types would always be serialized. To get around this
and be able to support schema concepts such as minOccurs="0" in XML
elements and use="optional" in Xml attributes, the XML serializer provides
a means to flag when you want a value type to be serialized or not.

In general, I like serializing thru fields, and programming thru
properties. Here's an example:

public class foo
{
[EditorBrowsable(EditorBrowsableState.Advanced)]
public string fieldFoo1;

[EditorBrowsable(EditorBrowsableState.Advanced)]
public string fieldFoo2;

[EditorBrowsable(EditorBrowsableState.Advanced)]
public int fieldFoo3;

[EditorBrowsable(EditorBrowsableState.Advanced)]
public boolean fieldFoo3Specified;

[XmlIgnore]
public string Foo1
{
get{ return fieldFoo1;}
set{ fieldFoo1 = value; }
}

[XmlIgnore]
public string Foo2
{
get{ return fieldFoo2; }
set { fieldFoo2 = value; }
}

[XmlIgnore]
public int Foo3
{
get { return fieldFoo3; }
set { fieldFoo3Specified = true; fieldFoo3 = value; }
}

public foo()
{
fieldFoo1 = String.Empty; // make the first field always have a
value
}
}

Now, when you serialize out of this class, you have always got a Foo1
element present. The remaining properties are optional. When you add to
this class, always add the public field at the end of the rest of the
public fields. This guarantees serialization order and your changes come
at the end. Then add a property to the class that is ignored by the XML
serializer.

The EditorBrowsable attribute on the fields means that for the most part,
intellisense helper tips won't expose the field members. This is good,
since you want the fields to be hidden from the programmer. They remain
public because the serializer today only serializes public data members on
classes that have public default constructors. The XmlIgnore attribute on
the properties makes sure that the serializer only outputs the field
content, and avoids the property setters completely.

An alternative way to do this is to only use fields, or only use public
properties and private fields. If you use properties, you must provide
both a read and write (get/set) behavior, elsewise the serializer will
ignore properties that do not have both getter and setter.

I hope this helps

Dan Rogers
Microsoft Corporation

--------------------
>Thread-Topic: Optional properties
>thread-index: AcTm2hWtV1KZThakSJOm/kEdin/vaA==
>X-WBNR-Posting-Host: 198.133.214.10
>From: =?Utf-8?B?QWxleCBEaW51?= <Alex http://www.velocityreviews.com/forums/(E-Mail Removed)>
>Subject: Optional properties
>Date: Mon, 20 Dec 2004 13:23:01 -0800
>Lines: 5
>Message-ID: <(E-Mail Removed)>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="Utf-8"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Content-Class: urn:content-classes:message
>Importance: normal
>Priority: normal
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
>Newsgroups: microsoft.public.dotnet.framework.aspnet.webservic es
>NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
>Path:

cpmsftngxa10.phx.gbl!TK2MSFTNGXA02.phx.gbl!cpmsftn gxa06.phx.gbl!TK2MSFTNGP08
phx.gbl!TK2MSFTNGXA03.phx.gbl
>Xref: cpmsftngxa10.phx.gbl

microsoft.public.dotnet.framework.aspnet.webservic es:27308
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservic es
>
>If you have a data class, Person, with two properties, FirstName and
>LastName, and at a later stage you want to add a new optional property to

the
>class which is used in a web service. What is the best way to achieve this

in
>order to minimize the effect on the cosumers, who may be running .NET and
>Java platforms.
>


 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      12-20-2004
What do you mean "optional" property? Properties are optional unless the
constructor requires an argument be passed into it and the class constructor
sets the property value.

To add an additional property to an existing class, you can just make a new
class that inherits the original and add your property:

Public Class NewDataClass
Inherits OldDataClass

private _newProp as something

Public Property NewProperty as something
Get
Return _newProp
End Get
Set(ByVal Value as something)
_newProp = Value
End Set

End Class


"Alex Dinu" <Alex (E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If you have a data class, Person, with two properties, FirstName and
> LastName, and at a later stage you want to add a new optional property to
> the
> class which is used in a web service. What is the best way to achieve this
> in
> order to minimize the effect on the cosumers, who may be running .NET and
> Java platforms.



 
Reply With Quote
 
Dan Rogers
Guest
Posts: n/a
 
      12-20-2004
Hi Scott,

I think he was referring to the XML schema notion of optional...

Dan
--------------------
>From: "Scott M." <(E-Mail Removed)>
>References: <(E-Mail Removed)>
>Subject: Re: Optional properties
>Date: Mon, 20 Dec 2004 17:46:25 -0500
>Lines: 34
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
>X-RFC2646: Format=Flowed; Original
>Message-ID: <(E-Mail Removed)>
>Newsgroups: microsoft.public.dotnet.framework.aspnet.webservic es
>NNTP-Posting-Host: 69.37.4.150.adsl.snet.net 69.37.4.150
>Path:

cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!cpmsftn gxa06.phx.gbl!TK2MSFTNGP08
phx.gbl!tk2msftngp13.phx.gbl
>Xref: cpmsftngxa10.phx.gbl

microsoft.public.dotnet.framework.aspnet.webservic es:27310
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservic es
>
>What do you mean "optional" property? Properties are optional unless the
>constructor requires an argument be passed into it and the class

constructor
>sets the property value.
>
>To add an additional property to an existing class, you can just make a

new
>class that inherits the original and add your property:
>
>Public Class NewDataClass
> Inherits OldDataClass
>
> private _newProp as something
>
> Public Property NewProperty as something
> Get
> Return _newProp
> End Get
> Set(ByVal Value as something)
> _newProp = Value
> End Set
>
>End Class
>
>
>"Alex Dinu" <Alex (E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> If you have a data class, Person, with two properties, FirstName and
>> LastName, and at a later stage you want to add a new optional property

to
>> the
>> class which is used in a web service. What is the best way to achieve

this
>> in
>> order to minimize the effect on the cosumers, who may be running .NET and
>> Java platforms.

>
>
>


 
Reply With Quote
 
Alex Dinu
Guest
Posts: n/a
 
      12-21-2004
Thanks. I think this will be very helpfull.

"Dan Rogers" wrote:

> Hi Alex,
>
> This is possible, from a code perspective, but you need to realize that
> this is a change in the contracted interface, so there are no guarantees
> that adding such a change at a later time will have zero impact on an
> existing caller. For instance, if some caller implementation took a copy
> of the WSDL at the time the contract between caller/callee was established,
> and uses the schemas in that WSDL to validate proper XML schema compliance,
> if you then later add properties to the class, and these get serialized,
> the validation check on the caller's part will fail - resulting in issues
> you may not anticipate.
>
> However, if you are among those folks that like to think of XML as a better
> comma delimited file, and you just seek out the elements you are looking
> for (say with xpath) or with an optimized pull parser (like that found in
> NET), then you can get away with this in most circumstances.
>
> The way to add more properties depends on their type. For reference types
> in .NET, just create a normal property or field, and place it at the end of
> your class. You need to take some care that you only serialize either thru
> fields or thru properties, and never mix them, since this can cause changes
> added at end to appear in the middle. This is due to the way the
> serializer first processes fields and then properties in order (or is it
> the other way around)...
>
> For reference types, they are not serialized if their values are null at
> runtime. For value types, such as bool and int, you have to do something
> slightly different. Value types never have a value, since as soon as you
> declare a value type, it is assigned a value as it is initialized. For
> this reason, value types would always be serialized. To get around this
> and be able to support schema concepts such as minOccurs="0" in XML
> elements and use="optional" in Xml attributes, the XML serializer provides
> a means to flag when you want a value type to be serialized or not.
>
> In general, I like serializing thru fields, and programming thru
> properties. Here's an example:
>
> public class foo
> {
> [EditorBrowsable(EditorBrowsableState.Advanced)]
> public string fieldFoo1;
>
> [EditorBrowsable(EditorBrowsableState.Advanced)]
> public string fieldFoo2;
>
> [EditorBrowsable(EditorBrowsableState.Advanced)]
> public int fieldFoo3;
>
> [EditorBrowsable(EditorBrowsableState.Advanced)]
> public boolean fieldFoo3Specified;
>
> [XmlIgnore]
> public string Foo1
> {
> get{ return fieldFoo1;}
> set{ fieldFoo1 = value; }
> }
>
> [XmlIgnore]
> public string Foo2
> {
> get{ return fieldFoo2; }
> set { fieldFoo2 = value; }
> }
>
> [XmlIgnore]
> public int Foo3
> {
> get { return fieldFoo3; }
> set { fieldFoo3Specified = true; fieldFoo3 = value; }
> }
>
> public foo()
> {
> fieldFoo1 = String.Empty; // make the first field always have a
> value
> }
> }
>
> Now, when you serialize out of this class, you have always got a Foo1
> element present. The remaining properties are optional. When you add to
> this class, always add the public field at the end of the rest of the
> public fields. This guarantees serialization order and your changes come
> at the end. Then add a property to the class that is ignored by the XML
> serializer.
>
> The EditorBrowsable attribute on the fields means that for the most part,
> intellisense helper tips won't expose the field members. This is good,
> since you want the fields to be hidden from the programmer. They remain
> public because the serializer today only serializes public data members on
> classes that have public default constructors. The XmlIgnore attribute on
> the properties makes sure that the serializer only outputs the field
> content, and avoids the property setters completely.
>
> An alternative way to do this is to only use fields, or only use public
> properties and private fields. If you use properties, you must provide
> both a read and write (get/set) behavior, elsewise the serializer will
> ignore properties that do not have both getter and setter.
>
> I hope this helps
>
> Dan Rogers
> Microsoft Corporation
>
> --------------------
> >Thread-Topic: Optional properties
> >thread-index: AcTm2hWtV1KZThakSJOm/kEdin/vaA==
> >X-WBNR-Posting-Host: 198.133.214.10
> >From: =?Utf-8?B?QWxleCBEaW51?= <Alex (E-Mail Removed)>
> >Subject: Optional properties
> >Date: Mon, 20 Dec 2004 13:23:01 -0800
> >Lines: 5
> >Message-ID: <(E-Mail Removed)>
> >MIME-Version: 1.0
> >Content-Type: text/plain;
> > charset="Utf-8"
> >Content-Transfer-Encoding: 7bit
> >X-Newsreader: Microsoft CDO for Windows 2000
> >Content-Class: urn:content-classes:message
> >Importance: normal
> >Priority: normal
> >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
> >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservic es
> >NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
> >Path:

> cpmsftngxa10.phx.gbl!TK2MSFTNGXA02.phx.gbl!cpmsftn gxa06.phx.gbl!TK2MSFTNGP08
> phx.gbl!TK2MSFTNGXA03.phx.gbl
> >Xref: cpmsftngxa10.phx.gbl

> microsoft.public.dotnet.framework.aspnet.webservic es:27308
> >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservic es
> >
> >If you have a data class, Person, with two properties, FirstName and
> >LastName, and at a later stage you want to add a new optional property to

> the
> >class which is used in a web service. What is the best way to achieve this

> in
> >order to minimize the effect on the cosumers, who may be running .NET and
> >Java platforms.
> >

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting properties (not necessarily "Properties") in Java 5.0 Wardie Java 4 11-08-2006 09:46 AM
CompositeControls: ViewState properties w/ Mapped properties probl =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?= ASP .Net 1 01-19-2006 09:19 AM
Making Custom Control Properties Visible in Visual Studio's Properties Palette Nathan Sokalski ASP .Net 0 10-17-2005 02:05 AM
Re: C++ properties Library Created (was Binding together Properties of Objects) Victor Porton C++ 1 08-29-2004 08:02 PM
Problems parsing when Properties.dtd.properties Kent Lichty Java 0 04-16-2004 03:08 PM



Advertisments