I need to save a class with list of countries in statics for caching.
the data is built with
string shortName //Primary Key - (IL or UK for example)
int
I suggest the use of static methods:
public class Country
{
public string ShortName {get;set;}
public int ID {get;set;}
public string LongName { get; set; }
}
public class Countries
{
static Dictionary dict = new Dictionary();
public static void Add(Country country)
{
if (!dict.ContainsKey(country.ShortName))
dict.Add(country.ShortName, country);
}
public static Country GetByShortName(string ShortName)
{
if (dict.ContainsKey(ShortName))
return dict[ShortName];
return null;
}
public static Country GetById(int id)
{
var result = from country in dict
where country.Value.ID==id
select new Country
{
ID = country.Value.ID,
ShortName = country.Value.ShortName,
LongName = country.Value.LongName
};
return result.SingleOrDefault();
}
public static List GetAll()
{
var result = from country in dict
select new Country
{
ID = country.Value.ID,
ShortName = country.Value.ShortName,
LongName = country.Value.LongName
};
return result.ToList();
}
}