WebServices - a discussion about how to return data
I haven't written many WebServices so I'm always looking for advice from people that have. Tonight I had a bit of a chat with Thomas Johansen about how to expose data from a service and I thought that I'd blog it... Oh yeh, I'm "We hates them" :-)
We hates them says:
how do you define the data for the Xml that you are going to return from a WebService?
Thomas Johansen says:
you just tap into the proxy, and it should be done for you - unless I misunderstood and you meant defining the data to be sent to the user
We hates them says:
yes, I mean defining the data to be sent to the user
Thomas Johansen says:
[WebMethod()]
public int GetAge()
{
return 99;
}
We hates them says:
right
Thomas Johansen says:
that was what you were after or?
We hates them says:
so, for a Person, you'd do that, what about the People collection?
We hates them says:
I'm only used to returning strong typed datasets
Thomas Johansen says:
you can send pretty much everything, if you want to send large binaries, you'd want to check out the WSE..
We hates them says:
so, Thomas...
We hates them says:
if I have a WebService to return info about People, this would be perfectly acceptable right? ...
We hates them says:
public class Person
{
int PersonId ;
string FirstName ;
string Surname ;
}
We hates them says:
[WebMethod("returns a person")]
public Person GetPerson( int personId )
{
return GetPersonById( personId ) ;
}
We hates them says:
[WebMethod("returns a person collection")]
public Persons GetAllPersons()
{
return GetAllPeople() ;
}
Thomas Johansen says:
that should work
We hates them says:
The reason that I like working with DataSets is because it's easy to report information back to the caller after they've attempted an update operation.
Thomas Johansen says:
yeah
Thomas Johansen says:
I've been starting to use dataset less and less myself though, using custom collections based upon ICollection and Hashtable
We hates them says:
public HashTable GetAllPeople()
{
HashTable ht = new HashTable() ;
// get all People
ICollection people = MyDatabaseCall() ;
foreach( Person p in people )
{
ht.Add( p.Id, p ) ;
}
return ht ;
}
Thomas Johansen says:
IMO, don't use foreach - AFAIK there is a performance hit associated with it.. use enumeration instead
We hates them says:
while( people.MoveNext()) )
Thomas Johansen says:
IEnumerator peopleEnum = MyDatabaseCall().GetEnumerator();
while
Thomas Johansen says:
yeh
We hates them says:
so, you would just return and accept HashTable's ????
We hates them says:
well, ICollections
Thomas Johansen says:
well I create custom collections though.. but yeah
We hates them says:
public class Persons : ICollection
{
// ICollection implementation here
}
Thomas Johansen says:
yeah,
or
public class Persons : ArrayList
...
public class Persons : CollectionBase
Thomas Johansen says:
it all depends on what you want to do and how much custom code you want of course
We hates them says:
minimum
Thomas Johansen says:
the easiest IMO is ArrayList
Thomas Johansen says:
got pretty much all you need
Thomas Johansen says:
even binarysearch
We hates them says:
yup, I love ArrayLists
We hates them says:
so, you would return and accept a raw ArrayList
Thomas Johansen says:
yeah, arraylists and hastables are the *best*
Thomas Johansen says:
We hates them says:
and, when receiving an ArrayList you would enumerate and Validate that each item was a CMyExpectedType
Thomas Johansen says:
yeah, perhaps using the 'as' keyword
We hates them says:
right
Thomas Johansen says:
or operator rather
We hates them says:
just let me check my C# skills here
Thomas Johansen says:
aah you're doing vb, thats right
We hates them says:
Person p = al.Next() as Person ;
if(!( p == null )) {}
We hates them says:
-- Next() being psuedo code
Thomas Johansen says:
or
if (p != null)
We hates them says:
ofc
Thomas Johansen says:
We hates them says:
I *always* code the negative instead of the affirmative ... it's a bad habit of mine