将 HasMany 和 ManyToMany 关系公开为 IEnumerable

发布于 2024-11-02 15:14:49 字数 1206 浏览 0 评论 0原文

目前,在我的实体中,我将集合公开为 IList,但我一直在考虑将它们公开为 IEnumerable,以防止用户手动添加到集合中。我为这些操作添加了特定的内容,以便我可以确保我的双向关系保持完整。在这种情况下,我想到了几个问题。

  1. 如果我将它们公开为 IEnumberable 是否意味着我需要为每个代表实体中关系的集合提供一个 Add 和 Remove 方法?
  2. 有没有更简单的方法来做到这一点?我并不反对这样做,只是想知道。
  3. 你这样做吗?

示例:

public class OrderHeader
{
    public virtual Guid OrderId { get; private set; }

    public virtual IList<OrderLine> OrderLines { get; set; }

    public virtual void AddLine(OrderLine orderLine)
    {
        orderLine.Order = this;
        OrderLines.Add(orderLine);
    }

    //No need for a remove method since we expose collection as IList
}

转换上面的类以便我们只公开 IEnumerable 将导致:

public class OrderHeader
{
    public virtual Guid OrderId { get; private set; }

    private IList<OrderLine> orderLines { get; set; }
    public IEnumerable<OrderLine> OrderLines { get { return orderLines; } }

    public virtual void AddLine(OrderLine orderLine)
    {
        orderLine.Order = this;
        orderLines.Add(orderLine);
    }

    public virtual void RemoveLine(OrderLine orderLine)
    {
        orderLines.Remove(orderLine);
    }
}

Currently in my entities I'm exposing my collections as an IList but I've been thinking about exposing them as a IEnumerable to prevent users from manually adding to the collections. I have specific adds for these operations so that I can make sure my bi-directional relationships stay intact. A couple questions come to mind in this scenario.

  1. If I expose them as IEnumberable does this mean I'll need an Add and Remove method for every collection that represents a relationship in my entities?
  2. Is there an easier way to do this? I'm not against doing it this way just wondering.
  3. Are you doing it this way?

Example:

public class OrderHeader
{
    public virtual Guid OrderId { get; private set; }

    public virtual IList<OrderLine> OrderLines { get; set; }

    public virtual void AddLine(OrderLine orderLine)
    {
        orderLine.Order = this;
        OrderLines.Add(orderLine);
    }

    //No need for a remove method since we expose collection as IList
}

Converting the class above so that we only expose IEnumerable would result in:

public class OrderHeader
{
    public virtual Guid OrderId { get; private set; }

    private IList<OrderLine> orderLines { get; set; }
    public IEnumerable<OrderLine> OrderLines { get { return orderLines; } }

    public virtual void AddLine(OrderLine orderLine)
    {
        orderLine.Order = this;
        orderLines.Add(orderLine);
    }

    public virtual void RemoveLine(OrderLine orderLine)
    {
        orderLines.Remove(orderLine);
    }
}

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

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

发布评论

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

评论(2

那一片橙海, 2024-11-09 15:14:49
  1. 是的,如果您公开 IEnumerable,最好在类上添加方法来处理添加/删除
  2. 私有支持字段是一个非常好的解决方案。
  3. 是的,但请记住,如果您想真正只读访问公开的集合,请使用 ReadOnlyCollection - http://msdn.microsoft.com/en-us/library/ms132474.aspx
  1. Yes, if you expose an IEnumerable it is best to add methods on the class to handle Add/Remove
  2. A private backing field is a pretty good solution.
  3. Yes, but keep in mind if you want truly read only access to the exposed collection use ReadOnlyCollection - http://msdn.microsoft.com/en-us/library/ms132474.aspx
落墨 2024-11-09 15:14:49

同意 Dan 的回答,稍作修改:

public IEnumerable<OrderLine> OrderLines
{ get { return orderLines.Select(x => x; } }

Agreed with Dan's answer, with a minor change:

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