简体   繁体   English

对象架构设计问题

[英]Object Architecture Design Question

I'm trying to figure out how to best lay this out. 我正在尝试找出如何最好地进行布局。 I'll explain what I have now, but I'm wondering if there is a better way to do this. 我将解释我现在拥有的东西,但是我想知道是否有更好的方法可以做到这一点。

I have a class Section that has basic properties: Name, Description, et.al. 我有一个具有基本属性的类Section:名称,描述等。 I use a list of Sections for the users to choose from. 我使用部分列表供用户选择。 They can add a Section as many times as they want to a Parent object and any number of Sections. 他们可以向父对象和任意数量的节中添加任意多个节。

When they add a Section to the parent, they have to assign what group it belongs to (lets say group1, group2, group3) and in what order it will be displayed. 当他们将节添加到父项时,他们必须分配其所属的组(例如,group1,group2,group3),并以什么顺序显示它。 This Group property is not on the table, there is no group needed when I list out the Sections for the user to choose from, it wouldn't make sense. 该组属性不在表中,当我列出各节供用户选择时,不需要任何组,这没有任何意义。 Think of this Section they are adding as a clone with extra properties. 想想这部分,他们将添加为具有额外属性的克隆。

I have another table that has a foreign key to the Parent and to the Section. 我有另一个表,该表具有到父级和该节的外键。 Many sections can be added to 1 Parent. 可以将许多部分添加到1个父级中。 On this link table is also the Grouping and DisplayOrder columns (as well as a few others) for each section that they add. 在此链接表上,还包括它们添加的每个部分的Grouping和DisplayOrder列(以及其他一些列)。

So when I create the Parent object and query a collection of its Sections, do I want to try and use the same Section class and add a Grouping property? 因此,当我创建Parent对象并查询其Sections的集合时,是否要尝试使用相同的Section类并添加Grouping属性?

 Section 1
 Section 2
 Section 3


Parent 1
     Section 1 - Group = g1, DisplayOrder = 1
     Section 1 - Group = g2, DisplayOrder = 2
     Section 2 - Group = g2, DisplayOrder = 3
     Section 3 - Group = g3, DisplayOrder = 4

Parent 2
     Section 4 - Group = g3, DisplayOrder = 1
     Section 1 - Group = g2, DisplayOrder = 2
     Section 2 - Group = g3, DisplayOrder = 3

Tell me if you have no idea what I'm saying and I'll try to explain it better...or I'll delete it and pretend like I never asked. 告诉我,如果您不知道我在说什么,我会尽力向您解释……或者我将其删除,然后装出我从未问过的东西。 =P = P

It sounds like the group isn't an intrinsic property of a section. 听起来好像组不是节的固有属性。 It's only meaningful when relating sections to parents. 仅在将节与父母相关时才有意义。 Given this, I wouldn't add a group property. 鉴于此,我不会添加组属性。 Maybe make a section container class that exposes the group: 也许制作一个暴露该组的节容器类:

interface IGroupedSection
{
    ISection Section { get; }
    string Group { get; }
}

or make grouping a property of parent, eg 或使分组成为父项的属性,例如

interface IParent
{
     void AddSection(string group, ISection section);
     IEnumerable<ISection> GetSectionsInGroup(string group);
}

You may need more than one section grouping strategy, after all, so it's best not to couple your section design to the types of collection it can be contained within. 毕竟,您可能需要多个节分组策略,因此最好不要将节设计与可以包含在其中的集合类型相结合。

When possible in object hierarchies, you want to avoid child to parent links. 在对象层次结构中尽可能避免使用子级到父级链接。 It makes for very tight coupling and awkward relationships. 这使得非常紧密的耦合和尴尬的关系。

Assuming I understand your problem, I would create a Group object to insert between the parents and the sections. 假设我了解您的问题,我将创建一个Group对象,以在父级和各节之间插入。 A parent would have a list of groups, and each group would have a list of sections. 父级将有一个组列表,每个组将有一个节列表。

Edit in response to DisplayOrder: 编辑以响应DisplayOrder:

Display order is actually a property of the parent, not the sections. 显示顺序实际上是父级的属性,而不是各节的属性。 A parent contains a list of sections, and that list has a particular order associated with it. 父级包含一个部分列表,并且该列表具有与之关联的特定顺序。

I would add the group property. 我将添加组属性。 A null value would be appropriate for a section that had not been assigned to a parent. 空值适用于尚未分配给父级的节。 This is similar to the behavior of Owner in windows forms components. 这类似于Windows窗体组件中Owner的行为。

I would make group a property of Section. 我将组设为Section的属性。 Then you can add properties for group1, group2, etc on Parent using LINQ to query the collection an return only those in each group (if you need to be able to do that). 然后,您可以使用LINQ在Parent上为group1,group2等添加属性,以查询集合,仅返回每个组中的属性(如果需要的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM