Library tutorials & articles
Data Integrity in Web Services
- Introduction
- Creating the web service
- The Test Client
- Conclusion
Creating the web service
The web service below is a simple web service that simply passes the struct PersonData back and forth assigning it to a static member. A useful extention of this example is to build up some object-relational mapping between our Person object and a database, but that is beyond the scope of this article.
The areas of note in this object are the PersonData struct and the Person object. Please note that YearsExperience does indeed have an accessor method. Also note that the Person object does a complete range checking on the struct as it is passed in before allowing an assignment.
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 20 characters"); }
if(value.LastName.Length > 20)
{ throw new Exception("LastName must be less than 20 characters"); }
if(value.YearsExperience < 2)
{ throw new Exception("People with less than 2 years exp are unemployable in IT."); }
personData=value;
}
}
//Other useful methods to operate on a person
}//END OF PERSON OBJECT
}//END of RemoteObject Passer Namespace
|
Related articles
Related discussion
-
MOS Protocol - Anyone used it?
by alexnavarro38 (5 replies)
-
Read HSQLDB data into a webpage
by joe90 (3 replies)
-
VB.NET Simple Client / Server Communication
by r0bbyw (3 replies)
-
Dot NET questions.
by amohanece (0 replies)
-
ListView-How to use it?
by imiazlina (8 replies)
Related podcasts
-
Remoting Pt. 2
Podcast (MP3): Download Hosts: Markus Michael Guests: Recording venue: This is the second part of the remoting infrastructures discussion started in Episode 9. We take a look at how remoting infrastructures such as CORBA, .NET Remoting or Web Service...
Events coming up
-
Dec
8
December Silicon Valley Ruby Meetup
Moffett Field, United States
In a World of Middleware, Who Needs Monolithic Applications? by Jon Crosby With Rack emerging as the standard for composing web applications and services, most recently with Rails adoption, an architectural shift is taking place. Learn how to create next generation web services by reusing existing Rack middleware and supplementing with your own components and micro-frameworks like Sinatra. Bio : Jon likes music, the Open Web, Ruby, Erlang, Haskell, Objective-C, JavaScript and coffee.
This thread is for discussions of Data Integrity in Web Services.