The Artima Developer Community
Sponsored Link

.NET Buzz Forum
The Web services empire strikes back - Support for Nullable and SqlTypes

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Christian Weyer

Posts: 616
Nickname: cweyer
Registered: Sep, 2003

Christian Weyer is an independent Microsoft MSDN Regional Director and expert for Web services.
The Web services empire strikes back - Support for Nullable and SqlTypes Posted: Oct 30, 2004 10:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Christian Weyer.
Original Post: The Web services empire strikes back - Support for Nullable and SqlTypes
Feed Title: Christian Weyer: Web Services & .NET
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/cweyer/Rss.aspx
Feed Description: Philosophizing about and criticizing the brave new world ...
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Christian Weyer
Latest Posts From Christian Weyer: Web Services & .NET

Advertisement

Part 1: The Web services empire strikes back - Introductory thoughts
Part 2: The Web services empire strikes back - Inner Workings
Part 3: The Web services empire strikes back - Web Services in Visual Studio 2005
Part 4: The Web services empire strikes back - WS-I BP Conformance
Part 5: The Web services empire strikes back - Custom XML Serialization
Part 6: The Web services empire strikes back - Proxy Type Sharing
Part 7: The Web services empire strikes back - Contract-first with .NET 'IDL'
Part 8: The Web services empire strikes back - Schema Importer Extensions
Part 9: The Web services empire strikes back - Making asynchronous Web service calls easier

Not too many of our customers have complained in the past that there is no way to express null values for value types in .NET. Well, this is just the concept of value types, isn’t it? But it is an essential piece that is missing when working with XML schema-based types that allow null values. The .NET Framework 2.0 provides a solution for this.
Take a look at the following XML fragment that denotes a valid order:

<OrderStatus>
  <Id>42</Id>
  <OrderDate>2004-07-07</OrderDate>
  <ShipDate xsi:nil="true"/>
</OrderStatus>

Again, imagine our fits-it-all Web service to deliver information about order status. Based on the query criteria, the service may return the status of an order that has not been shipped yet. In this case the Web service returns a ShipDate element with the XSD instance attribute nil set to true.
The current version of the .NET Framework, or more correctly the proxy class generation and serialization process, already supports this. When you have an XSD or WSDL with a nillable or optional element, the proxy class contains an additional flag. In our example above this would be ShipDateSpecified. This flag indicates whether the value for this element is set or not. People need to be able to differentiate whether the element or attribute was missing from the XML document and whether the attribute was present but had the default value. The support for truly nullable types in the .NET Framework would make things a lot easier. Take a look at the following Web service code:

[WebServiceBinding(
 ConformanceClaims = WsiClaims.BP10,
 EmitConformanceClaims = true)]
[WebService(Namespace = "
http://www.thinktecture.com/demos/Nullable")]
public class NullableService
{
 [WebMethod]
 [return:XmlElement("MyData", IsNullable=true)]
 public SqlInt32 GetData(bool ReturnNull)
 {
    SqlInt32 mydata;
    mydata=(ReturnNull ? SqlInt32.Null : 1234);
   
  return mydata;
 }
}

This WebMethod returns a type of SqlInt32. After a quick look into the documentation we can see that it implements INullable. This is the essential interface that has to be supported when implementing your own nullable types. If we point wsdl.exe to the service’s interface and message contract it will generate the following code snippet:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute(
"http://www.thinktecture.com
/demos/Nullable/GetData",
RequestNamespace="http://www.thinktecture.com
/demos/Nullable",
ResponseNamespace="http://www.thinktecture.com
/demos/Nullable",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("MyData", IsNullable=true)]
public System.Nullable<System.Int32> GetData(bool ReturnNull)
{
  object[] results = this.Invoke("GetData", new object[]
    {ReturnNull});
  return ((System.Nullable<System.Int32>)(results[0]));
}

As can be seen very clear, the proxy uses a generic type System.Nullable<System.Int32> to indicate that the actual instance of the return type may be null.
SqlTypes is also an example of a type that implements IXmlSerializable. The SQL types like SqlInt32 can then emit their own schemas and serialize/deserialize themselves. If we specify a special switch on proxy generation, the wsdl.exe tool can map nillable elements of primitive types (e.g. Int32, Date) to SqlTypes. So if you want to consume the above service, you might run wsdl.exe with the /sqltypes switch to get a proxy class that contains this code:

[return: System.Xml.Serialization.XmlElementAttribute("TheData",
IsNullable=true)]
public SqlInt32 GetData(bool ReturnNull)
{
  object[] results = this.Invoke("GetData", new object[]
    {ReturnNull});
  return ((System.Data.SqlTypes.SqlInt32)(results[0]));
}

Now we have a SqlInt32 data type present as return type of the method. This can come in handy when you are developing database-oriented and a bit more tightly coupled systems than it originally was intended by the Web services credo.

Read: The Web services empire strikes back - Support for Nullable and SqlTypes

Topic: Ireland .Net Developers Alliance - Register and have a chance to win an Xbox :-) Previous Topic   Next Topic Topic: 828,000 Windows users can't be wrong, can they?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use