[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 782
  • Last Modified:

Web services and associative arrays

Hello,
We're creating a web service that allows retreieval of NFL player statistics.  We were going to use PHP, but now we are going to use C# since setting up the web service seems to be easier.  I originally planned to return the result set in a associative array in PHP.  For example for http://www.nfl.com/players/playerpage/492974 I would have something like:
array
(
  "first_name" => "Julius",
  "last_name"  => "Jones",
  "weight" => "205",
   ...
   "rushing" => array("stats" => array("Team", "G","GS", "Att", "Yards", "Avg", "Lg", "TD", "20+", "FD"),
                                     "2004" => array(....)
                                    )
   ...
)

What would be the best way to implement this in C#?  My main goal is an easy way to provice access to a result set.  Is it possible to return a class in such a manner?

Thanks,
Yasir
0
Yasir_Malik
Asked:
Yasir_Malik
1 Solution
 
CrazyIvan007Commented:
The best way to do this is to create a new class of CollectionBase


public class PlayerSets : CollectionBase
{
      public void Add(PlayerSet _ps)
      {
            this.List.Add(_ps);
      }

      public void Remove(PlayerSet _ps)
      {
            this.List.Remove(_ps);
      }

      public PlayerSet Find(string StringToFind)
      {
            PlayerSet _returnSet = null;
            
            foreach (PlayerSet _ps in this.List)
            {
                  // Include your own Find Createria if you want
            }

            return (_returnset);
      }
}

public class PlayerSet
{
      private string _FirstName = null;
      private string _LastName = null;

      ...

      public string FirstName
      {
            get { return _FirstName; }
            set { _FirstName = value; }
      }

      public string LastName
      {
            get { return _LastName; }
            set { _LastName = value; }
      }
}

First you create a CollectionBase with Add/Remove/Find, then you create a new class of your Datasets. After that you can simply call up your Dataset with

PlayerSets _playersets = new PlayerSets();

To Add new items to them

PlayerSet _player = new PlayerSet();
_player.FirstName = "First Name";
_player.LastName = "Last Name";
_playersets.Add(_player);

You can cygle through the sets

foreach (PlayerSet _player in _playersets)
{ }

and implement new Features and Find Algorithms. I think that would be the best for your needs.
0
 
Yasir_MalikAuthor Commented:
It looks like you can't return Hashtables.  I'm not that familiar yet with web services, but will another application program written in, say, PHP be able to deal with what you have?
0
 
CrazyIvan007Commented:
You can return Hashtables.. you can use a Hashtable Set

Hashtable hTable = new Hashtable();

hTable["key"] = value;

or

PlayerSet _player = new PlayerSet();
// Set the _player Vars

hTable["key"] = _player;


and later derefernce the object to your Set

PlayerSet _player = (PlayerSet)hTable["key"];


But using CollectionBase is more accurate :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NipNFriar_TuckCommented:
If you do what CrazyIvan007 said but use the DictionaryBase you in essence have a strongly typed dictionary.
0
 
edj371Commented:
It seems like you have some hierarchical data that you need to pass around, and it sounds like you may still be interested in PHP.

Do you want to keep from tying yourself down to return values and data types specific to a language?  Have you thought about xml, and considered that it might be the right tool for the job?

It not only crosses languages (really good XML parsers abound in nearly all languages), but it will allow you to create data flow that is independent of language, since the interfaces are in xml.  You can query xml and return values as with a dictionary or collections of values.  Really, for hierarchical data that you'll be passing around, xml is the way to go.  In fact, DataSets in C# are merely xml with nice wrapper classes.  Another nice feature of xml is, given an xml node, like the I've included below, it would be very easy, if desired, to slap an xsl transform on it and turn it into some good looking html.  Then all you have to do is modify the xsl transform and get different html, keeping your xml structure the same.

I have included the xml node below as an initial, and very rough, pass at encapsulating your data.  The structure of your xml will depend on what you want to do with the data it holds, and how you want to query it.

<Player>
 <Bio>
  <FirstName>Julius</FirstName>
  <LastName>Jones</LastName>            
  <Position>RB</Position>
  <Height>5-9</Height>
  <Born>08/14/1981</Born>
  <College>Notre Dame</College>
  <NFLExperience>Rookie</NFLExperience>
 </Bio>
 <Stats>
  <Season>
   <Year>2004</Year>
   <Team>
    <Name>Dallas Cowboys</Name>            
    <G>3</G>
    <GS>2</GS>
    <Rushing>
     <Att>68</Att>
     <Yards>247</Yards>
     <Avg>3.6</Avg>
     <Lg>33</Lg>
     <TD>2</TD>
     <Over20>1</Over20>
     <FD>12</FD>
    </Rushing>
    <Receiving>
     <No>3</No>
     <Yards>13</Yards>
     <Avg>4.3</Avg>
     <Lg>7</Lg>
     <TD>0</TD>
     <Over20>0</Over20>
     <Over40>0</Over40>
     <FD>1</FD>
    </Receiving>
   </Team>
  </Season>
 </Stats>
</Player>
0
 
Yasir_MalikAuthor Commented:
Thank you all for your response.
What we ended up implementing was a just a regular class.  I guess we might as well have used structs.  Hashtables were not allowed as return types because they could not be serialized by SOAP.  Matrices ([,]) could not also be serialized, so I wasn't sure that the CollectionBase interface would work either.  Here is a sample of our XML output:
<?xml version="1.0" encoding="utf-8"?>
<PlayerInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
  <bio>
    <position>FS</position>
    <firstName>Roy</firstName>
    <lastName>Williams</lastName>
    <number>31</number>
    <height>6-0</height>
    <DOB>08/14/1980</DOB>
    <weight>226</weight>
    <college>Oklahoma</college>
    <experience>3</experience>
    <id>302238</id>
  </bio>
  <tables>
    <StatisticTable>
      <tableName>DEFENSIVE STATS</tableName>
      <columnNames>
        <string>Year</string>
        <string>G</string>
        <string>Total</string>
        <string>Tckl</string>
        <string>Ast</string>
        <string>Sacks</string>
        <string>Int</string>
        <string>Yds</string>
        <string>Avg</string>
        <string>Lg</string>
        <string>TD</string>
        <string>Pass Def</string>
      </columnNames>
      <teamsPlayedFor>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
      </teamsPlayedFor>
      <statistics>
        <ArrayOfDouble>
          <double>2002</double>
          <double>16</double>
          <double>99</double>
          <double>88</double>
          <double>11</double>
          <double>2</double>
          <double>5</double>
          <double>90</double>
          <double>18</double>
          <double>85</double>
          <double>2</double>
          <double>3</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2003</double>
          <double>16</double>
          <double>72</double>
          <double>55</double>
          <double>17</double>
          <double>2</double>
          <double>2</double>
          <double>69</double>
          <double>34.5</double>
          <double>39</double>
          <double>0</double>
          <double>6</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2004</double>
          <double>11</double>
          <double>62</double>
          <double>52</double>
          <double>10</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>6</double>
        </ArrayOfDouble>
      </statistics>
      <totalStatistics>
        <double>43</double>
        <double>233</double>
        <double>195</double>
        <double>38</double>
        <double>4</double>
        <double>7</double>
        <double>159</double>
        <double>22.7</double>
        <double>85</double>
        <double>2</double>
        <double>15</double>
      </totalStatistics>
    </StatisticTable>
    <StatisticTable>
      <tableName>FUMBLES</tableName>
      <columnNames>
        <string>Year</string>
        <string>G</string>
        <string>Fum</string>
        <string>Lost</string>
        <string>Fum Forced</string>
        <string>Own Rec</string>
        <string>Opp Rec</string>
        <string>Yards</string>
        <string>Tot Rec</string>
        <string>TD</string>
      </columnNames>
      <teamsPlayedFor>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
      </teamsPlayedFor>
      <statistics>
        <ArrayOfDouble>
          <double>2002</double>
          <double>16</double>
          <double>0</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2003</double>
          <double>16</double>
          <double>0</double>
          <double>0</double>
          <double>2</double>
          <double>0</double>
          <double>1</double>
          <double>0</double>
          <double>1</double>
          <double>0</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2004</double>
          <double>11</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
        </ArrayOfDouble>
      </statistics>
      <totalStatistics>
        <double>43</double>
        <double>0</double>
        <double>0</double>
        <double>5</double>
        <double>0</double>
        <double>4</double>
        <double>0</double>
        <double>4</double>
        <double>0</double>
      </totalStatistics>
    </StatisticTable>
  </tables>
</PlayerInfo>

We are also implementing a cache.  Does anyone know why the asp_web.exe service just hangs when trying to read from a file?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now