Data Integrity in Web Services

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

You might also like...

Comments

About the author

David Talbot United States

David Talbot is an experienced Software Architect with a diverse background including creating network applicances, working with television set top boxes, building Billing/CRM systems, Web Port...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra