如何存储设计时数据列表

发布于 2024-11-29 07:26:34 字数 898 浏览 2 评论 0原文

我的数据中有以下结构:

Category0  
 -SubCategory0  
 -SubCategory1  
 -SubCategoryN  

Category1  
-SubCategory1_0  
-SubCategory1_1  
-SubCategory1_N  

类别将有一个名称、一个描述和一个唯一的整数 ID,

例如

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  

列表在设计时已知,我需要绑定到列表视图。我想将描述绑定为列表视图每一行上的显示文本,并将值(具有名称和 Id 的对象或枚举)绑定为相应的值成员。

存储此信息的最佳方法是什么?我应该创建大量枚举吗?或者我应该使用分隔字符串(例如“Ford:Mondeo:Some Text: 12324”)在设计器模式下直接绑定到列表视图,然后根据需要进行解析和提取。也许最好将数据存储为具有 id/description 值的自定义属性的强类型枚举,例如绑定到字典,其中 string 是描述,CarType 是具有属性的类:Make(Ford):enum, Model(Modeo) ):枚举和 Id(12324):int?

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 to 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱纠缠 2024-12-06 07:26:34

通常,您会使用两个类对此进行建模:

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; }
}

如果您担心比较中的性能,并且您确切地知道所有制造商和型号名称,则应该考虑将名称更改为枚举。
另外,如果您要按名称访问这些项目,则应考虑将它们保存在字典中,并以名称作为键。

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.

海拔太高太耀眼 2024-12-06 07:26:34

这听起来像是 XML 的完美应用。您可以添加/删除类别、更改名称和类别的值。描述等。将其解析为简单的类结构...

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; } 
}

然后您只需将这些类绑定到您的视图即可。

仅仅因为它在设计时就已知,并不足以成为创建大量重复、冗余代码的理由。它使你的程序更难维护。是的,对于那些对 XML(或任何数据文件)感到不舒服的人来说,查看起来更简单,但是如果有什么变化 - 例如,您需要向类别添加另一个属性 - 您将需要去更改每个类/枚举。又乱又乏味。

编辑:只是为了澄清一下,当我说 XML 时,这只是我的首选格式。您还可以将数据存储为文本、CSV 或任何您喜欢的格式。我更喜欢 XML,因为它更容易使用。

编辑2:
我明白您的担忧 (if(carListView.SelectedValue == "Mondeo"))。在不知道(或不想知道)您的整个系统或您想要做什么的情况下,我更愿意以更通用、以对象为中心的方式工作。

那么每种类型的汽车都需要一个 if 语句吗?为什么不让汽车自己做事呢?

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文