【问题标题】:.NET Business Object Collection (List) [closed].NET 业务对象集合(列表)[关闭]
【发布时间】:2015-11-30 21:38:10
【问题描述】:

我想创建一个业务对象集合类(T 列表)。我的业务对象有一个 Deleted 属性(所以我可以稍后跟踪数据库删除)。集合类需要覆盖 List 的 remove、for-each 枚举方法等,以便我可以将业务对象标记为已删除,而不是从列表中“物理”删除,并在枚举期间跳过它们。到目前为止,我有一个继承 List(of BusObject) 的类,但我发现一些 List 方法(即 Remove)是不可覆盖的。我接近这个错误吗?也许我不应该继承 List 并使用自己的方法在内部管理列表?

【问题讨论】:

    标签: .net oop inheritance


    【解决方案1】:

    您可以像这样实现自己的ICollection

    如果您想写入数据库等,可以使用InternalCollection 属性访问内部集合,包括已删除的项目。

    选择为CopyTo 方法抛出NotSupportedException,因为在那里实现什么并不明显(我们是否也想复制已删除的项目?)。

    class SoftDeleteCollection<T> : ICollection<T>
        where T : class, ISoftDelete
    {
        public ICollection<T> InternalCollection { get; private set; }
    
        public SoftDeleteCollection()
        {
            this.InternalCollection = new List<T>();
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            return this.InternalCollection.Where(i => !i.IsDeleted).GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    
        public void Add(T item)
        {
            this.InternalCollection.Add(item);
        }
    
        public void Clear()
        {
            foreach (T item in this.InternalCollection.Where(item => !item.IsDeleted))
            {
                item.IsDeleted = false;
            }
        }
    
        public bool Contains(T item)
        {
            return this.InternalCollection.Any(i => i == item && !i.IsDeleted);
        }
    
        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotSupportedException();
        }
    
        public bool Remove(T item)
        {
            if (this.Contains(item))
            {
                item.IsDeleted = true;
                return true;
            }
            return false;
        }
    
        public int Count
        {
            get { return this.InternalCollection.Count(item => !item.IsDeleted); }
        }
    
        public bool IsReadOnly
        {
            get { return false; }
        }
    }
    

    【讨论】:

      【解决方案2】:

      看看从 ObservableCollection 继承。这可能会对您有所帮助。

      类参考:https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

      排序类似的线程,其中有一个有用的评论:C# Custom Observable Collection - Should I use Composition or Inheritance?

      【讨论】:

        【解决方案3】:

        Collection&lt;T&gt; 类继承并覆盖适当的方法,例如RemoveItemClearItems。 看看GetEnumerator

        更多信息请访问MSDN

        当我想到这一点时,我认为另一种方法可能更简单,尤其是在迭代集合和跳过软删除的项目时: 通过继承Collection&lt;T&gt; 创建您自己的集合类。删除项目时,您不仅应设置 IsDeleted 标志,还应从集合中删除该项目,并将其添加到集合类中定义的其他集合中。

        像这样:

        public class MyBusinessCollection<T> : Collection<T> where T : BusObject
        {
            private readonly List<T> _deletedItems = new List<T>();
        
            protected override RemoveItem( int index )
            {
               var item = this[index];
        
               item.Deleted = true;
        
               _deletedItems.Add(item);
        
               base.RemoveItem(index);
            }
        
            public IEnumerable<T> GetDeletedItems()
            {
                 return _deletedItems;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-07
          • 1970-01-01
          • 2019-03-09
          • 1970-01-01
          • 2019-02-03
          • 2012-10-09
          • 1970-01-01
          • 2010-12-20
          相关资源
          最近更新 更多