我应该在Hierarcical对象中具有Parent还是ListOfChild属性 [英] Should i have Parent or ListOfChild property in Hierarcical object

查看:75
本文介绍了我应该在Hierarcical对象中具有Parent还是ListOfChild属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何建模用于存储标记树的分层对象有疑问:

i have a doubt on how to model a hierarchical object for storing a Tag tree:

我可以使用的数据库表

public class Tag
{
    public int Id { get; set; }
    public int Description { get; set; }
    private readonly Tag parentTag;
    public Tag ParentTag
    {
        get
        {
            return parentTag;
        }
    }
}

来自父财产的代码来了来自分层对象和AutoFixture 以避免循环引用

the code from parent property come from Hierarchical object and AutoFixture to avoid circular reference

,但是按照那里的建议,最好是考虑使用子标签而不是父标签来反对:自然来说,一个标签可以包含一个子标签集合就更有意义
我的课程变为:

but as suggested there, and thinking to object maybe is better having child collection instead of parent: naturally speaking a tag can have a collection of child tags make more sense So my class become:

public class Tag
{
    public int Id { get; set; }
    public int Description { get; set; }
    private IList<Tag> childTag;
    public IEnumerable<Tag> ChildTag
    {
        get
        {
            return childTag.remove(0);
        }
    }
}

但是用这种方式我在树中移动了一个标签:这就是更改标签的父级属性吗?

but in this way how can i move a tag in the tree: that is change Parent property on my tag?

EDIT

思考我的对象将要做什么:

thinking about what my object will have to do I would say:


  • 创建新根目录

  • 创建新孩子

  • 移动节点

  • 判断节点是否为孩子

  • 说一个节点是否是根节点

  • (递归)获取节点的所有子节点

  • 删除子节点(递归且不是)

  • create new root
  • create new child
  • move a node
  • say if a node is a child
  • say if a node is a root
  • get all child of a node (recursivly)
  • delete child (recursivly and not)

我必须允许用户创建标签树来为存储的文档加标签,以实现类似于lightroom用于图片的标签收集的功能

I have to allow the user to create trees of Tags for tagging documents that are stored would like to achieve something similar to the tag collection used by lightroom for pictures

推荐答案

孩子是否有 Parent 属性,或者父母的孩子属性应取决于您的代码需要执行的操作。

Whether you have a Parent property on the child, or a Children property on the parents, should depend on what your code needs to do.

尝试以下操作:删除两个属性,看看是否有人在乎。

Try this: remove both properties and see if anyone cares.

响应您的更新。我尽可能使用测试驱动的开发。测试成为程序需要做什么的可执行定义(A:它需要通过所有测试)。然后,使测试通过的过程将迫使您以仍然可以通过所有测试的最简单方式来实现代码。

Responding to your update. I use test-driven development whenever possible. The tests become an executable definition of "what does the program need to do" (A: it needs to pass all the tests). The process of making the tests pass then forces you to implement your code in the simplest way possible that still passes all the tests.

首先编写一组调用您的类的客户端代码,然后仅实现使客户端代码正常工作所需的内容。我建议您删除有问题的两个属性,然后开始编写代码。您将快速确定是否需要一个或两个属性。

You can do the same thing without TDD by first writing a set of client code that calls your class, then only implementing what's required in order for the client code to work. I recommend that you remove both properties in question, then start writing your code. You will quickly determine whether or not you need one, or both, properties.

这篇关于我应该在Hierarcical对象中具有Parent还是ListOfChild属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆