Professional Web Applications Themes

Optional properties - ASP.NET Web Services

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....

  1. #1

    Default Optional properties

    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.
    Alex Dinu Guest

  2. #2

    Default RE: Optional properties

    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 pr (like that found in
    NET), then you can get away with this in most cirstances.

    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 [email]Dinudiscussions.microsoft.com[/email]>
    >Subject: Optional properties
    >Date: Mon, 20 Dec 2004 13:23:01 -0800
    >Lines: 5
    >Message-ID: <E21E4413-E867-4F7B-BC7F-F83CAF890222microsoft.com>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > cht="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.
    >
    Dan Rogers Guest

  3. #3

    Default Re: Optional properties

    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 [email]Dinudiscussions.microsoft.com[/email]> wrote in message
    news:E21E4413-E867-4F7B-BC7F-F83CAF890222microsoft.com...
    > 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.

    Scott M. Guest

  4. #4

    Default Re: Optional properties

    Hi Scott,

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

    Dan
    --------------------
    >From: "Scott M." <s-marnospam.nospam>
    >References: <E21E4413-E867-4F7B-BC7F-F83CAF890222microsoft.com>
    >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: <eWIWPVu5EHA.1596tk2msftngp13.phx.gbl>
    >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 [email]Dinudiscussions.microsoft.com[/email]> wrote in message
    >news:E21E4413-E867-4F7B-BC7F-F83CAF890222microsoft.com...
    >> 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.
    >
    >
    >
    Dan Rogers Guest

  5. #5

    Default RE: Optional properties

    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 pr (like that found in
    > NET), then you can get away with this in most cirstances.
    >
    > 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 [email]Dinudiscussions.microsoft.com[/email]>
    > >Subject: Optional properties
    > >Date: Mon, 20 Dec 2004 13:23:01 -0800
    > >Lines: 5
    > >Message-ID: <E21E4413-E867-4F7B-BC7F-F83CAF890222microsoft.com>
    > >MIME-Version: 1.0
    > >Content-Type: text/plain;
    > > cht="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.
    > >
    >
    >
    Alex Dinu Guest

Similar Threads

  1. ViewState properties and mapped properties don't work well togethe
    By Christophe Peillet in forum ASP.NET Building Controls
    Replies: 2
    Last Post: January 21st, 07:35 PM
  2. Making Custom Control Properties Visible in Visual Studio's Properties Palette
    By Nathan Sokalski in forum ASP.NET Building Controls
    Replies: 0
    Last Post: October 17th, 02:05 AM
  3. Optional Arguments in a CFC
    By Cannikinn in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: June 22nd, 09:16 PM
  4. numberformat - is optional no longer optional?
    By miki in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 18th, 11:07 PM
  5. properties not showing in properties window at design time
    By Peter in forum ASP.NET Building Controls
    Replies: 2
    Last Post: December 10th, 03:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139