How to Store a list of Design-Time data
I have the following structure in my data:
Category0
-SubCategory0
-SubCategory1
-SubCategoryN
Category1
-SubCategory1_0
-SubCategory1_1
-SubCategory1_N
A category will have a NAME, a Description and a Unique Integer ID
e.g.
Category = Ford Description = "USA Car" Id = 12345678
-SubCategory: Name = Mondeo Description = "Some text" Id = 12324
-SubCategory: Name = Fiesta Description = "Some text" Id = 9999
-SubCategory: Name = Orion Description = "Some text" Id = 123456
-SubCategory: Name = Focus Description = "Some text"Id = 8799
The list is known at design time and I need to bind to the listview. I'd like to bind the Description as the Display Text on each line of the listview and the values(an object or an enum with the Name and Id) as the corresponding valuemember.
What is the best method to store this info? Should I create a large number of enumerations? Or should I bind directly to the listview in designer mode using delimited strings such as "Ford:Mondeo:Some Text: 12324" and then parse and extract as needed. Perhaps it would be better t开发者_开发知识库o have the data stored strongly typed enums with custom attributes for the id/description values e.g bind to a dictionary where string is a description and CarType is a class with properties: Make(Ford):enum, Model(Modeo):enum and Id(12324):int?
Typically you would model this with two classes:
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Manufacturer
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Model> Models { get; set; }
}
If you are concerned about performance in the comparisons, and you know exactly all the manufacturer and model names, you should consider changing the names into enums. Also, if you will be accessing these items by name, you should consider keeping them in a dictionary with the name as the key.
This sounds like a perfect use for XML. You can add / remove categories, change the values of the name & description etc. Parse it into a simple class structure...
public class ParentCategory : Category
{
public List<Category> SubCategories { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
And then you simply bind these classes to your view.
Just because it's known at design time is not a good enough reason to go and create tons of duplicated, redundant code. It makes your program so much harder to maintain. Yes, it's simpler to look at for someone uncomfortable with XML (or any data file) but if something changes - you need to add another property to the categories, for example - you'll need to go and change every single class / enum. Messy and tedious.
edit: Just to clarify, when I say XML that's just my preferred format. You can also store your data as text, CSV, whatever your favourite format. I prefer XML as it's easier to work with.
edit2:
I see your concern (if(carListView.SelectedValue == "Mondeo")
). Without knowing (or wanting to know) your whole system or what you're trying to do, I'd prefer to work in a more generic, object focused fashion.
So you'll need an if
statement for each type of car? Why not just get the car to do its own work?
public class ParentCategory : Category
{
public List<Category> SubCategories { get; set; }
public void DoThings()
{
// look at SubCategories, print something, etc
}
}
// and then get the item to do things!
((ParentCategory)carListView.SelectedValue).DoThings();
This way there's no looping through whole lists. Again, keep your number of lines down.
精彩评论