I'm going to use the example of ISO 3166-1 country definitions for this. In the standard, you may have 3 values: the alpha2 country code "CA"
, the alpha3 country code "CAN"
, and the numeric code "040"
. All of these map to the country Canada
. I mustn't be able to use an external data source or load a text file at random.
I can store these in a dictionary as below:
enum Country { Canada, UnitedStates };
void Main()
{
Dictionary<string, Country> countries = new Dictionary<string, Country>();
countries["CA"] = countries["CAN"] = countries["040"] = Country.Canada;
}
The problem with this is that I would have to add a line for each country that I want to add. That's a lot of country codes to type in, and a lot of enum values to create.
It seems like what I want to do is have an "in-code database" that is compiled into the assembly. One option would be to add a resources file and parse the file every time, but that seems too slow and wasteful. Storing the parsed collection in binary format seems attractive.
How can I store these 3 values as well as a full country name for every recorded country inside of my program? Is there a logical and well-known design pattern for dealing with this data problem?