I am trying to design a class of static objects. For example lets assume they are to represent car models. This is how I started out:
public class CarModel
{
internal CarModel(string manufacturer, string modelName, double seconds0To60, double maxMPH)
{
Manufacturer = manufacturer;
ModelName = modelName;
Seconds0To60 = seconds0To60;
MaxMPH = maxMPH;
}
public string Manufacturer { get; private set; }
public string ModelName { get; private set; }
public double Seconds0To60 { get; private set; }
public double MaxMPH { get; private set; }
public override string ToString() { return Manufacturer + " " + ModelName; }
static public readonly CarModel AlfaRomeo_Brera = new CarModel("Alfa Romeo", "Brera 1.75 TBi 3d", 7.5, 146.0);
static public readonly CarModel AlfaRomeo_Giulietta = new CarModel("Alfa Romeo", "Giulietta 1.4 TB Lusso 5d", 9.1, 121.0);
static public readonly CarModel Ford_Focus = new CarModel("Ford", "Focus 2.5 RS 3d", 5.2, 163.0);
static public readonly CarModel Ford_Mondeo = new CarModel("Ford", "Mondeo Saloon 2.0 Zetec 4d", 9.7, 130.0);
static public readonly CarModel Honda_Accord = new CarModel("Honda", "Accord Tourer 2.4 i-VTEC EX 5d (Adas)", 7.6, 138.0);
static public readonly CarModel Honda_Civic = new CarModel("Honda", "Civic Hatchback 1.8 i-VTEC Type S 3d Auto", 10.6, 127.0);
}
This approach seemed to work well for the 6 test models as above. However, it now seems that I have approximately 500 car models to input and there are many properties for each car model. The car data I have is currently in an Excel spreadsheet. So the question is how best to add this data to my dll?
I would like all car models to be compiled into the assembly dll if that is possible. So I would prefer not to use a database. I had a brief look at some dynamic enum posts – perhaps some automatic code generation might work? Or perhaps I could copy and paste my data into a resource file? Or maybe there is someway to add a DataSet or DataTable to the project that contains this static data?
I think the static readonly properties in the example class above will need to be changed for a more sophisticated approach to accessing the list of models.
Please let me know your suggestions,
Thanks.
Since you said you would prefer the models to be compiled into the assembly, I would look into T4. Here is a tutorial http://msdn.microsoft.com/en-us/library/dd820614.aspx. The basic approach is to:
Every time you build your project, the t4 template will run, generating the class for you.
Edit, here is a sample .tt file which solves the problem: