【问题标题】:Many to Many Relationship Objects in C#C# 中的多对多关系对象
【发布时间】:2010-12-07 06:36:58
【问题描述】:

我正在使用 .NET 2.0。我有 2 个对象,一个是:PhoneService,另一个是 ChartOfAccount。这之间的关系是多对多的。

public class PhoneService
{
    private List<ChartAccount> chartAccounts
    private ChartAccount chartAccount

    public Int64 ID
    {
     get { return id; }
     set { id = value; }
    }

    public bool Add()
    { ... }

    public bool Update()
    { ... }
}

public class ChartAccount
{
    public Int64 ID
    {
     get { return id; }
     set { id = value; }
    }

    public bool Add()
    { ... }

    public bool Update()
    { ... }

    public bool Allocate()
    {
       // this will save data for the bridge table only
    }
}

现在我的问题是,当你这样做时是否可能:

PhoneService service = new PhoneService();
service.ChartAccounts[0].ID = 5
service.ChartAccounts[1].ID = 10

由于 ChartAccounts 是一个集合,我该如何尝试把 Allocate() 变成这样: service.ChartAccounts.Allocate()

我能想到的只是将 Allocate() 方法放在 Service 类中。有什么想法吗?

谢谢

【问题讨论】:

    标签: .net


    【解决方案1】:

    通常,多对多对象(如在数据库中)本身应该是对象。

    为什么不使用第三个对象来表示多对多链接,而不是在对象中?

    【讨论】:

    • 我宁愿根据上下文选择其中一个。
    【解决方案2】:

    虽然这可能不是最干净的方法,但您始终可以只创建一个仅包含图表帐户列表的 ChartAccounts 类,并将该方法添加到该类。另一种选择是在 chatAccount 类上创建一个扩展方法来执行分配代码。

    【讨论】:

      猜你喜欢
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2017-01-22
      • 2020-12-17
      • 2015-03-16
      相关资源
      最近更新 更多