What C# datatype should be used for a partially constant set of information?

I'm writing a C# application that does some things with baseball statistics. I'm getting the data from an XML web service. I want to completely separate the presentation-business-data layers. In that vain I have created a service class to interact with the web service and to create objects from the data, a set of business classes (i.e. player, team, league), and the objects needed for my presentation layer.

I don't want to load the statistics into the player class because there are more than 200 statistics, and I'm not going to need them every time I instantiate the player class. For each statistic I need its abbreviation, description, XML attribute name, and value.

Question: What C# data-structures or method could be used to present more than two properties and allow several of them to be static across all instances of the datatype and allow at least one to be writable at run-time?

I started to create a "statistic" class like this:

public class BaseballStatistic
{
    public string Abbreviation;
    public string Description;
    public string XmlAttributeName;
    public string Value;

    public BaseballStatistic(string abbreviation, string description, string xmlAttributeName)
    {
        Abbreviation = abbreviation;
        Description = description;
        XmlAttributeName = xmlAttributeName;
    }
}

The problem with this is that I'll never need to change the abbreviation, description, or XML attribute name at run-time.

  • If I only needed the abbreviation and value, I'd use a Dictionary or some other Key/Value pair.
  • I started to create a statistic class with abbreviation, description, XML attribute name, and value members. But the only one of those that will ever change at runtime is the value - so this didn't feel like the right answer. (Imagine 500 players, each with an array of statistic objects, and those object are the exact same for all players except for the value.)
  • I considered creating a multidimensional array, but with so many constant values, it seems wasteful to load such a thing at run time.

I feel like I should know this.


I should add this: because there are so many different statistics that I can use, it would be great if I could find a solution that would expose them to Intellisense. For example:

Statistic g = Stats.GamesPlayed

Jon Skeet
people
quotationmark

It sounds like you need one class for "a statistic" - and then a Dictionary<PlayerStat, int> (or whatever the value would be).

The PlayerStat class would know about the abbreviation, description and XML attribute name - and I'd suggest that you probably create a Dictionary<string, PlayerStat> statically to map from abbreviation to statistic, and another one to map from XML attribute to abbreviation.

You may not even need the Dictionary<PlayerStat, int> in your Player class... you could always add an indexer or method which looked things up lazily. (Be careful with this, however - you may find that loading many things lazily will be more expensive than loading everything in one go. You may want to break the statistics into categories, and load all the stats for a single category when you load one of them. Basically, if there are clumps of stats that are usually used together...)

people

See more on this question at Stackoverflow