本文介绍了将HasMany和ManyToMany关系公开为IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的实体中,我将我的集合作为IList公开,但我一直在考虑将它们公开为IEnumerable,以防止用户手动添加到集合中。我对这些操作有具体的补充,以便我可以确保我的双向关系保持不变。在这种情况下,有几个问题想起来。


  1. 如果我将它们暴露为IEnumberable,这是否意味着我需要一个Add和Remove方法来表示每个集合中的一个关系?

  2. 有没有更简单的方法来做到这一点?我不反对这样做只是想知道。

  3. 您是这样做的吗?

      public class OrderHeader 
    {
    public virtual Guid OrderId {get;私人设置}

    公共虚拟IList< OrderLine> OrderLines {get;组; }

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

    //因为我们将集合公开为IList
    }



    转换上面的类以便我们只暴露IEnumerable会导致:

    pre $ code public class OrderHeader
    {
    public virtual Guid OrderId {get;私人设置}

    私有IList< OrderLine> orderLines {get;组; }
    public IEnumerable< OrderLine> OrderLines {get {return orderLines; }}

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

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


    $ / code $ / pre

    解决方案

    1. 是的,如果你公开一个IEnumerable,最好在这个类上添加方法来处理Add / Remove

    2. 私有后台是一个很好的解决方案。 li>
    3. 是的,但请记住,如果你想真正只读访问暴露的集合使用ReadOnlyCollection -


    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);
        }
    }
    
    解决方案
    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

    这篇关于将HasMany和ManyToMany关系公开为IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:51