关联父级和子级数据模型对象的最佳方法是什么?

发布于 2024-10-16 08:20:52 字数 870 浏览 7 评论 0原文

在父子之间建立关联时,我下面的代码看起来非常复杂。

问题:关联父级和子级数据模型对象的最佳方式是什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ConsoleApplication1;

class Child
{
    public string Name { get; set; }
    public Parent Parent { get; set; }
}


class Parent
{
    public string Name { get; set; }
    public IEnumerable<Child> Children { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        Parent p1 = new Parent { Name = "P1" };

        Child c1 = new Child { Name = "C1" };
        c1.Parent = p1;

        Child c2 = new Child { Name = "C2" };
        c2.Parent = p1;


        List<Child> children = new List<Child>();
        children.Add(c1);
        children.Add(c2);

        p1.Children = children;
    }

}

My code below looks so complicated when making association between Parent and Child.

Question: What is the best way to associate Parent and Child data model object?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ConsoleApplication1;

class Child
{
    public string Name { get; set; }
    public Parent Parent { get; set; }
}


class Parent
{
    public string Name { get; set; }
    public IEnumerable<Child> Children { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        Parent p1 = new Parent { Name = "P1" };

        Child c1 = new Child { Name = "C1" };
        c1.Parent = p1;

        Child c2 = new Child { Name = "C2" };
        c2.Parent = p1;


        List<Child> children = new List<Child>();
        children.Add(c1);
        children.Add(c2);

        p1.Children = children;
    }

}

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

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

发布评论

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

评论(3

朮生 2024-10-23 08:20:52

也许您应该在类中实现一些辅助方法,其中封装了一些基本思想:

public class Parent {

....
    public void AddChild(Child child) {
        child.Parent = this;
        this.Children.Add(child);
    }

    public void RemoveChild(Child child) {
        child.Parent = null;
        this.Children.Remove(child);
    }
}


public class Child {
    private Child() {}

    public static Child CreateChild(Parent parent) {
        var child = new Child();
        parent.AddChild(child);
        return child;
    }
}

Maybe you should implement some helper methods into the class, which encapsulate some basic thinks:

public class Parent {

....
    public void AddChild(Child child) {
        child.Parent = this;
        this.Children.Add(child);
    }

    public void RemoveChild(Child child) {
        child.Parent = null;
        this.Children.Remove(child);
    }
}


public class Child {
    private Child() {}

    public static Child CreateChild(Parent parent) {
        var child = new Child();
        parent.AddChild(child);
        return child;
    }
}
北座城市 2024-10-23 08:20:52

这个问题可能会有所帮助:

C# 中的树数据结构

This question might help:

Tree data structure in C#

失退 2024-10-23 08:20:52
class Parent
{
  ...
  public Child AddChild(string name)
  {
    return this.AddChild(new Child(name));
  }

  public Child AddChild(Child c)
  {
    c.Parent = this;
    this.children.Add(c);

    return c;
  }
}
class Parent
{
  ...
  public Child AddChild(string name)
  {
    return this.AddChild(new Child(name));
  }

  public Child AddChild(Child c)
  {
    c.Parent = this;
    this.children.Add(c);

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