自定义集合

.NET集合位于命名空间System.Collections下,.NET的“内置”集合划分成了三种类别:

 * 有序集合:仅仅实现ICollection接口的集合,在通常情况下,其数据项目的插入顺序控制着从集合中取出对象的的顺序。 System.Collections.Stack System.Collections.Queue类都是ICollection集合的典型例子。

   * 索引集合:实现Ilist的集合,其内容能经由从零开始的数字检索取出,就象数组一样。System.Collections.ArrayList对象是索引集合的一个例子。

   * 键式集合:实现IDictionary接口的集合,其中包含了能被某些类型的键值检索的项目。IDictionary集合的内容通常按键值方式存储,可以用枚举的方式排序检索。 System.Collections.HashTable类实现了IDictionary接口。

    这是微软封装好的,但最好要写自己的集合,因为自定义集合会给咱带来很多好处,人家封装好的,你想扩展其他方法做不到。使用自定义集合的好处,我大概想到了几点:

  易维护:改变需求,也只是改变局部模块,其他不动。替换就OK

  易扩展:由于继承、封装、多态的特性,自然设计出高内聚、低耦合的系统结构,使得系统更灵活、更容 易扩展,而且成本较低。

  代码重用:不用重复为一些对象写方法。

这些好处我感觉就是面向对象的一些好处,自定义集合就是面向对象的发展。

下面是我写的一个小示例:

结构图:


MyCollection类:

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

namespace ExamCollection
{
    public abstract class MyCollection<T> : CollectionBase, IEnumerable<T>
    {
        #region 判断集合中是否存在某元素
        /// <summary>
        /// 判断集合中是否存在某元素
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        public virtual bool Exists(Predicate<T> match)
        {
            ExceptionHelper.FalseThrow<ArgumentNullException>(match != null, "match");

            bool result = false;

            foreach (T item in List)
            {
                if (match(item))
                {
                    result = true;
                    break;
                }
            }

            return result;
        }
        #endregion

        #region 在集合中查找满足匹配条件的第一个元素
        /// <summary>
        /// 在集合中查找满足匹配条件的第一个元素
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        public virtual T Find(Predicate<T> match)
        {
            ExceptionHelper.FalseThrow<ArgumentNullException>(match != null, "match");

            T result = default(T);

            foreach (T item in List)
            {
                if (match(item))
                {
                    result = item;
                    break;
                }
            }

            return result;
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        protected virtual void Add(T obj)
        {
            ExceptionHelper.FalseThrow<ArgumentNullException>(obj != null, "obj");
            List.Add(obj);
        }
        #endregion

    }
}


DictionaryCollection类:

using ITOO.Exam.BLLFactory;
using ITOO.Exam.IBLL;
using ITOO.Exam.Model;
using MCS.Library.Core;
using MCS.Library.Data.DataObjects;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExamCollection
{
    public  class ExamDictionaryCollection<T> : EditableDataObjectCollectionBase<T> where T :new()
    {
        //小容器集合
        public IList listCollection { get; set; }

        //字典类型ID
        private string dictionaryTypeId;
        /// <summary>
        /// 字典类型ID
        /// </summary>
        public string DictionaryTypeId
        {
            get { return dictionaryTypeId; }
            set { this.dictionaryTypeId = value; }
        }

        public ExamDictionaryCollection(string dictionaryTypeId)
        {
            this.dictionaryTypeId = dictionaryTypeId;
        }

        public void DictionaryTest()
        {

        }

        /// <summary>
        /// 往集合类里添加对象
        /// </summary>
        /// <param name="data"></param>
        public void Add<Tobj>(Tobj obj)
        {
            ExceptionHelper.FalseThrow<ArgumentNullException>(obj != null, "obj");
            this.listCollection.Add(obj);
            
            //base.Add(data);
        }


        public bool AddDictionary(MyCollectionBase<ExamDictionaryEntity> dictionaryCollection)
        {
            //集合应该单独出来,才调D层B层方法
            //B层字典接口
            IExamDictionaryEntityBLL dictionaryBLL = BLLAbstractFactory.GetExamDictionaryEntityBLL();            
            //B层字典类型接口
            IExamDictionaryTypeEntityBLL dictionaryTypeBLL = BLLAbstractFactory.GetExamDictionaryTypeEntityBLL();
            
            //MyCollectionBase<ExamDictionaryEntity> dictionaryCollection2 = dictionaryBLL.QueryDictionary2();

            //T dicTypeCollection = new T();

            //将字典存放到数据库
            foreach (var item in dictionaryCollection)
            {
                item.ExamDictionaryTypeEntityDictionaryTypeId=this.dictionaryTypeId;
                dictionaryBLL.Add(item);

            }

            return true;
        }

    }
}


    这样,我们添加字典时,只要实例化一个字典容器,将字典集合放如字典容器集合即可。如果我们想给所以字典加一个方法,只要写的字典容器里即可。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值