Adding Static Data to a C# Project
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开发者_Python百科 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:
- define your models in csv
- create a t4 template which reads the csv, writes the data at the top of the class(internal CarModel, etc), iterates through the data, writing out "static public your property" for each line
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:
<#@ template debug="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text.RegularExpressions" #>
namespace Play.Helpers
{
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; }
<#
String path = "D:\\My Documents\\Visual Studio 2010\\Projects\\Play\\Play\\Content\\testdata.csv";
List<string[]> parsedData = new List<string[]>();
try
{
using (StreamReader readFile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
#>
static public readonly CarModel <#=(String)row[0].Replace(" ", "_")#>_<#=Regex.Replace(row[1], @"[\.\(\)-]", "_").Replace(" ", "_")#> = new CarModel("<#=(String)row[0]#>", "<#=row[1]#>", <#=row[2]#>, <#=row[3]#>);
<#
parsedData.Add(row);
}
}
}catch(Exception e)
{
//left as an excercise for the reader
}
#>
}
}
Put the models in a file of somesort and add the file as Content to the project. When building, copy the file to the destination directory. The file format can be fixed width, XML, CSV, or whatever format is easier. When the DLLs loads, load the file which will be all the car models.
I would drop the static variable approach.
You could use enumerations for the manufactuer and model, but new manufacturers and models would require a code change.
精彩评论