主细节对象在 OOP 中如何工作?
我目前正在设计一个主细节网格视图。我为它们定义了两个单独的对象。父 gridview 绑定到第一个对象,在行数据绑定事件中,我使用父 ID 将第二个 gridview 与详细对象绑定。
我在互联网上看到的很多例子都初始化父类中的详细信息对象,
public class Author
{
private string name;
public BooksList books=new BooksList();
public BooksList Books
{
get
{
return emailAddresses;
}
set
{
emailAddresses = value;
}
}
在内部初始化子类的目的是什么 父类?
目前,我只是将父级绑定到一个单独的对象&孩子到一个 在我的表示层中手动分离对象永远不要在父类中初始化子对象。是不是方法不对 对于面向对象编程?
- 为我的 gridview 完成此任务的推荐方法是什么?
达米安.
I am currently designing a master detail gridview. I have two separate objects defined for them. The parent gridview is binded to the first object and in row databind event I bind the second gridview with the detail object using parent ID.
A lot of examples I see on the internet initialize the details object inside the parent class,
public class Author
{
private string name;
public BooksList books=new BooksList();
public BooksList Books
{
get
{
return emailAddresses;
}
set
{
emailAddresses = value;
}
}
What is the purpose of initializing child class inside the
parent class?Currently, I just bind the parent to a separate object & child to a
separate object manually in my presentation layer & never initialize child objects inside parent class. Is that the wrong way
for OOP ?- What is the recommended way to achieve this task for my gridview?
Damien.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1:大多数时候,您希望在类内初始化对象以避免空引用问题。
2:你在这里混淆了你的概念,因为你如何呈现你的对象与 OOP 并不真正相关。等式的 OOP 部分是关于类是什么以及它们如何相互关联,所以是的,如果父类和子类之间存在关系,那么它应该在类结构中表示。
3:理想情况下,您将拥有一个类结构,该结构允许您绑定到数据网格并可以自动为数据网格生成层次结构。
1: Most of the time you want to initialize you objects inside of your class to avoid null reference issues.
2: You confusing you concepts here, as how you present your objects is not really relevant to OOP. The OOP part of the equation is all about what your classes are and how they relate to each other, so yes if there is a relationship between the parent and child classes, then it should be represented in the class structures.
3: Ideally, you would have a class structure that allows you to bind to your datagrid and can automatically generate your hierarchy for your datagrid.
您是否认为作者不/不应该负责维护他/她的图书清单?什么书?他是唯一作者还是共同作者?你会如何解决这个问题?
我可以建议一个“图书馆”类,它有一个所管理的书籍列表,您可以借出、签入等。每本书都有一个或多个作者参考文献。您可以在图书类中实例化它们,但我认为如果它们是对图书馆作者列表的引用会更好。这样,每个作者只存在一次,而不是在他(合)写的每一本书中都有一个实例。
Have you considered that an Author is not/should not be responsible for maintaining a list of his/her books? What books? Whas (s)he the sole author or a co-author? How would you solve that?
May I suggest a "Library" class which has a list of books in its care, which you can check out, and check in, etc. Each book has one or more author references. You could instantiate these in the book class, but I think it would be better if they were references (in)to the Library's list of authors. That way each author only exists once, instead of having an instance in each and every book (s)he has (co-)written.
如果您想强制所有作者每次都有一个书单(无论是否为空),那么您可以这样做。否则,如果您想在许多情况下考虑使用 null 作为 BookList 以避免创建不需要的空对象,那么您可以单独创建 Booklist,并在情况需要时通过构造函数将特定的 booklist 实例注入到作者中,或者稍后设置它根据您的需要使用设置器。
IF you want to enforce that all authors have a booklist (empty or not) everytime then you do it like this. Otherwise if you want to consider having a null as BookList in many cases to avoid creating unneeded empty objects then you can create the Booklist separetly and inject a specific booklist instance it into the authors via the constructor when the case requires it, or set it later using the setter when depending on your necessities.