Data Integrity in Web Services (Turn One)

Source: Internet
Author: User
Tags range web services wrapper visual studio wsdl
Services|web Abstract
Web Services bring with them great possibilities and with these possibilities are some. One such pitfall is passing complex data types to and from Web Services without losing data integrity. The clearest thing to keep in mind when passing objects to Web Services are the data is passed for your object ' s fields, BU T the code is not.

What happens when I have an object, my Web service, passes as a return value?
WSDL does some magic when a programmer creates a referance to your Web service. Visual Studio.NET creates wrapper objects around data foreign.

The struct create inside your Web Service looks like this:

public struct Persondata
{
private int yearsexperience;
public int Yearsexperience
{
get {return yearsexperience;}
Set
{
if (value<2) {throw new Exception ("You ' re unemployable!");}
Yearsexperience = value;
}
}


Public String FirstName;
Public String LastName;
}



... Which then gets translated into WSDL Which as this:

<s:complextype name= "Persondata" >
<s:sequence>
<s:element minoccurs= "1" maxoccurs= "1"
Name= "FirstName" nillable= "true" type= "s:string"/>
<s:element minoccurs= "1" maxoccurs= "1"
Name= "LastName" nillable= "true" type= "s:string"/>
<s:element minoccurs= "1" maxoccurs= "1"
Name= "Yearsexperience" type= "S:int"/>
</s:sequence>
</s:complexType>


... to the client of the Web service, Visual Studio creates a wrapper based upon the WSDL that looks like this:

public struct Persondata
{
public int yearsexperience;
Public String FirstName;
Public String LastName;
}



And to make matters worse, as this struct gets passed to the server with yearsexperience=1 (A value that persondata.year Sexperience should not have) it'll be passed silently and without a exception! The solution to this bug, I mean feature, are to wrap all data so you want passed to and from a Web service inside a Stru CT and then in turn a validator class.

The struct is the carrier of the "data between" points and the object does all of the range checking required to keep yo ur data clean.

The Web service below is a simple Web service this simply passes the struct persondata back and forth assigning it to a St ATIC member. A useful extention of this example are to builds up some object-relational mapping between we person object and a database, But is beyond the scope of this article.

The areas of this object are the Persondata struct and the person object. Please note that Yearsexperience does indeed have a accessor method. Also Note this person object does a complete range checking on the struct as it are passed in before allowing a assign ment.

To start this project go to File->new project and then choose a asp.net Web Service from the selection menu. The complete code listing is below:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.Web;
Using System.Web.Services;

Namespace Remoteobjectpasser
{

public class PersonService:System.Web.Services.WebService
{
static person person;

Public Personservice ()
{
InitializeComponent ();
if (person==null)
{
person = new person ();
Persondata servicedata = new Persondata ();

Servicedata.firstname= "David";
Servicedata.lastname= "Talbot";
servicedata.yearsexperience=5;

Person. Currentdata=servicedata;
}
}

private void InitializeComponent () {}

protected override void Dispose (bool disposing) {}

[WebMethod]
Public Persondata Getpersondata ()
{return person.} CurrentData; }

[WebMethod]
public void Setpersondata (Persondata PD)
{this.person.CurrentData = PD;}

}//end of Personservice Object

public struct Persondata
{
private int yearsexperience;
public int Yearsexperience
{
get {return yearsexperience;}
Set
{
if (value<2) {throw new Exception ("You ' re unemployable!");}
Yearsexperience = value;
}
}

Public String FirstName;
Public String LastName;
}

public class Person
{
Private Persondata persondata;

Public Persondata CurrentData
{
get {return persondata;}
Set
{
if (value. Firstname.length > 20)
{throw new Exception ("FirstName must be less than");}
if (value. Lastname.length > 20)
{throw new Exception ("LastName must be less than");}
if (value. Yearsexperience < 2)
{throw new Exception ("People with less than 2 years exp are unemployable in IT.");
Persondata=value;
}
}
Useful methods to operate in a person
}//end of person OBJECT

}//end of RemoteObject Passer Namespace




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.