对象建模问题
我有汽车类,每个类都有额外的列表(皮革、腹肌、折叠镜等...)
public Extra
{
public bool Paid {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}
现在,更好的是:
class Car
{
public Extra Leather {get;set;}
public Extra FoldingMirrors {get;set;}
public Extra Abs {get;set;}
}
或更好:
class Car
{
private List<Extra> _listOfExtras=new List<Extra>
public List<Extra> ListOfExtras
{
...
}
}
最糟糕的部分:
我的 winforms 应用程序离线工作,每月发送一个给我的数据。
我必须在此应用程序中保留可用的附加功能列表(为每个汽车用户选择附加功能),因此我认为更好的方法是选项一。
如何在我的应用程序中保留此附加列表?它每月仅与网络服务连接一次。
I have got class Car, and each class has list of extras (leather, abs, Folding mirrors etc...)
public Extra
{
public bool Paid {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}
And now, better is:
class Car
{
public Extra Leather {get;set;}
public Extra FoldingMirrors {get;set;}
public Extra Abs {get;set;}
}
or better:
class Car
{
private List<Extra> _listOfExtras=new List<Extra>
public List<Extra> ListOfExtras
{
...
}
}
And the worst part:
My winforms application works offline and one per month is sending datas for me.
I must hold list Of available (foreach car user choose extras) extras in this application so better way I think is option one.
how hold this list of extras in my applications ? It connect with webservice only one time per month.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言(看到这是主观的),我会使用 Extra 的接口(例如 IExtra)。我还会选择第二个“选项”,即在 Car 类中包含额外内容的列表/字典。
如果需要,可以像选项 1 一样公开它们,只需在列表/字典中查找值即可。
Personally (seeing this is subjective), I would use an interface for Extra (eg IExtra). I would also go for the second 'option' of having a list/dictionary of extras contained in the Car class.
If needed, they can be exposed like in option 1, by just looking up the value in the list/dictionary.
额外的列表或字典(为了更容易访问)会更灵活。
这意味着,当在汽车中添加和/或删除附加功能时,您不必修改
Car
类,而如果每个附加功能都是显式添加的,则必须这样做。有多种方法可以在本地存储列表。您可以序列化到一个文件 - 纯文本或 XML,这会更好,因为您有为您执行序列化的类,或者您可以将其保存在本地数据库中。
A list or dictionary (for easier access) of extras would be more flexible.
It would mean that as extras were added and/or removed from a car you wouldn't have to modify the
Car
class which you would have to do if each were added explicitly.There are various ways to store the list locally. You could serialise to a file - either plain text or XML which would be better as you've got classes that do the serialisation for you, or you could hold it in a local database.
我会将其存储在
Dictionary
中。您不希望相同的额外内容多次出现...或者至少多次为它们付费:-)另外,Extra 应该是一个接口(或至少是一个抽象类)..
至于存储,您可以将 Car 类序列化为 XML。
I would store it in a
Dictionary<string, Extra>
. You don't want the same extras showing up more than once... Or at least paying for them more than once :-)Also, Extra should be an interface (or at the very least an abstract class)..
As for storage, you could serialize the Car class out to XML.