C#之泛型集合类(Ilist,IDictionary)的使用

时间:2020-11-24 17:00:46

IDictionary是键/值对的泛型集合的基接口,每个元素都是存在keyValuepair对象中的键值对。

每一对都必须有唯一的键。实现在是否允许 key 为 空引用(在 Visual Basic 中为 Nothing) 方面有所不同。此值可以为 空引用(在 Visual Basic 中为 Nothing),并且不必是唯一的。IDictionary 接口允许对所包含的键和值进行枚举,但这并不意味着任何特定的排序顺序。

C# 语言中的 foreach 语句(在 Visual Basic 中为 For Each,在 C++ 中为 for each)需要集合中每个元素的类型。由于 IDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型。

 

以下是一个简单的示例程序

 

实体类:

namespace Domain
{

//订单
    public class Order
    {
        public int OrderID { get; set; }

        /// <summary>
        /// 订单日期
        /// </summary>
        public DateTime OrderDate { get; set; }

        /// <summary>
        /// 订单地址
        /// </summary>
        public string OrderAdress { get; set; }

        /// <summary>
        /// 订单电话
        /// </summary>
        public string OrderTel { get; set; }
    }
}

 

//订单明细


namespace Domain
{
    public class OrderDetail
    {
        public int DetailID { get; set; }

        public int ProductID { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public decimal Quantity { get; set; }

        /// <summary>
        /// 单价
        /// </summary>
        public decimal Price { get; set; }
    }
}

 

//存放数据

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

namespace Service
{
    public static class OrderStock
    {
        private static IList<Order> m_orderList = new List<Order>(); //定义了一个List的泛型字段

        public static IList<Order> OrderList//定义了一个List的泛型属性
        {
            get
            {
                return m_orderList;
            }
            set
            {
                m_orderList = value;
            }
        }

        private static IDictionary<Order, IList<OrderDetail>> m_orders = new Dictionary<Order, IList<OrderDetail>>();

//定义了一个Dictionary的泛型字段,

        public static IDictionary<Order, IList<OrderDetail>> Orders/定义了一个Dictionary的泛型属性
        {
            get
            {
                return m_orders;
            }
            set
            {
                m_orders = value;
            }
        }
    }
}

 

服务接口

using System;
using Domain;
namespace Service
{
    public interface IOrderService
    {

   //删除
        void Delete(Domain.Order entity);

       //查询所有
        System.Collections.Generic.IList<Domain.Order> LoadAll();

   //保存
        object Save(Domain.Order entity);

   //更新
        void Update(Domain.Order entity);

    //根据ID查询
        Order Get(object id);

   //获取下一个ID
        object GetNextID();
    }

 

namespace Service
{
    public interface IOrderDetailService
    {
        void Delete(System.Collections.Generic.KeyValuePair<Domain.Order, System.Collections.Generic.IList<Domain.OrderDetail>> entity);
        System.Collections.Generic.KeyValuePair<Domain.Order, System.Collections.Generic.IList<Domain.OrderDetail>> Get(object id);
        object GetNextDetailID(object mainID);
        object GetNextID();
        System.Collections.Generic.IDictionary<Domain.Order, System.Collections.Generic.IList<Domain.OrderDetail>> LoadAll();
        object Save(Domain.Order entity, System.Collections.Generic.IList<Domain.OrderDetail> detail);
        void Update(System.Collections.Generic.KeyValuePair<Domain.Order, System.Collections.Generic.IList<Domain.OrderDetail>> entity);
    }
}

 

服务实现

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

namespace Service
{
    public class OrderService : IOrderService
    {
        public IList<Order> LoadAll()
        {
            var list = OrderStock.OrderList;

            return list;
        }

        public object GetNextID()
        {
            int id = 1;
            if (OrderStock.OrderList.Count > 0)
            {
                id = OrderStock.OrderList.Max(m => m.OrderID) + 1;
            }
            return id;
        }

        public object Save(Order entity)
        {
            OrderStock.OrderList.Add(entity);

            return entity.OrderID;
        }

        public void Update(Order entity)
        {
            var list = OrderStock.OrderList.Where(w => w.OrderID == entity.OrderID);

            if (list.Count<Order>() > 0)
            {
                Order order = list.First();
                OrderStock.OrderList[OrderStock.OrderList.IndexOf(order)] = entity;
            }
        }

        public Order Get(object id)
        {
            Order entity = null;
            var list = OrderStock.OrderList.Where(w => w.OrderID == (int)id);
            if (list.Count<Order>() > 0)
            {
                Order order = list.First();
                entity = OrderStock.OrderList[OrderStock.OrderList.IndexOf(order)];
            }
            return entity;
        }

        public void Delete(Order entity)
        {
            var list = OrderStock.OrderList.Where(w => w.OrderID == entity.OrderID);
            if (list.Count<Order>() > 0)
            {
                Order order = list.First();
                OrderStock.OrderList.Remove(order);
            }
        }
    }
}

 

 

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

namespace Service
{
    public class OrderDetailService : IOrderDetailService
    {
        public IDictionary<Order, IList<OrderDetail>> LoadAll()
        {
            var dic = OrderStock.Orders;

            return dic;
        }

        public object GetNextID()
        {
            int id = 1;
            if (OrderStock.Orders.Count > 0)
            {
                id = OrderStock.Orders.Max(m => m.Key.OrderID) + 1;
            }
            return id;
        }

        public object GetNextDetailID(object mainID)
        {
            int id = 1;
            var dic = OrderStock.Orders.Where(w => w.Key.OrderID == (int)mainID);
            if (dic.Count<KeyValuePair<Order, IList<OrderDetail>>>() > 0)
            {
                var item = dic.First();
                var list = item.Value.Where(w => w.DetailID == (int)mainID);
                if (list.Count<OrderDetail>() > 0)
                {
                    id = list.First().DetailID + 1;
                }
            }
            return id;
        }

        public object Save(Order entity, IList<OrderDetail> detail)
        {
            object result = null;
            if (!OrderStock.Orders.ContainsKey(entity))
            {
                OrderStock.Orders.Add(entity, detail);
                result = entity.OrderID;
            }
            return result;
        }

        public void Update(KeyValuePair<Order, IList<OrderDetail>> entity)
        {
            if (OrderStock.Orders.ContainsKey(entity.Key))
            {
                OrderStock.Orders[entity.Key] = entity.Value;
            }
        }

        public KeyValuePair<Order, IList<OrderDetail>> Get(object id)
        {
          
            KeyValuePair<Order, IList<OrderDetail>> entity = new KeyValuePair<Order, IList<OrderDetail>>();
            var dic = OrderStock.Orders.Where(w => w.Key.OrderID == (int)id);
            if (dic.Count<KeyValuePair<Order, IList<OrderDetail>>>() > 0)
            {
                entity = dic.First();
            }
           
            return entity;
        }

        public void Delete(KeyValuePair<Order, IList<OrderDetail>> entity)
        {
            if (OrderStock.Orders.ContainsKey(entity.Key))
            {
                OrderStock.Orders.Remove(entity.Key);
            }
        }
    }
}

 

测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Service;
using Domain;

namespace ServiceTest
{
    [TestFixture]
    public class OrderServiceTest
    {
        private IOrderService m_service;

        [SetUp]
        public void Init()
        {
            m_service = new OrderService();
        }

        [Test]
        public void LoadAllTest()
        {
           var list= m_service.LoadAll();
           foreach (var item in list)
           {
               Console.WriteLine(item.OrderID);
               Console.WriteLine(item.OrderAdress);
               Console.WriteLine(item.OrderDate);
               Console.WriteLine(item.OrderTel);
           }
           Assert.IsNotNull(list);
        }

        [Test]
        public void Save()
        {
            Order entity = new Order()
            {
                OrderID = (int)m_service.GetNextID(),
                OrderDate = DateTime.Now.AddDays(-1),
                OrderAdress = "乌鲁木齐",
                OrderTel = "123123"
            };
            object obj = m_service.Save(entity);
          
            Assert.IsNotNull(obj);
        }

        [Test]
        public void Update()
        {
            Order entity = m_service.Get(1);
            entity.OrderAdress = "库尔勒";
            m_service.Update(entity);
        }

        [Test]
        public void Delete()
        {
            Order entity = m_service.Get(1);
            entity.OrderAdress = "库尔勒";
            m_service.Delete(entity);
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Service;
using Domain;

namespace ServiceTest
{
    [TestFixture]
    public class OrderServiceDetailTest
    {
        private IOrderDetailService m_service;

        [SetUp]
        public void Init()
        {
            m_service = new OrderDetailService();
        }

        [Test]
        public void LoadAllTest()
        {
           var list= m_service.LoadAll();
           foreach (var item in list)
           {
               Console.WriteLine("--------------订单----------");
               Console.WriteLine(item.Key.OrderID);
               Console.WriteLine(item.Key.OrderAdress);
               Console.WriteLine(item.Key.OrderDate);
               Console.WriteLine(item.Key.OrderTel);
               Console.WriteLine("--------------订单明细----------");
               foreach (var li in item.Value)
               {
                   Console.WriteLine(li.DetailID);
                   Console.WriteLine(li.Price);
                   Console.WriteLine(li.Quantity);
                   Console.WriteLine(li.ProductID);
               }
           }
           Assert.IsNotNull(list);
        }

        [Test]
        public void Save()
        {
            int id = (int)m_service.GetNextID();
            Order entity = new Order()
            {
                OrderID = id,
                OrderDate = DateTime.Now.AddDays(-1),
                OrderAdress = "乌鲁木齐",
                OrderTel = "123123"
            };
            IList<OrderDetail> list = new List<OrderDetail>();
            for (int i = 0; i < 3; i++)
            {
                list.Add(new OrderDetail() {
                    DetailID = i + 1,
                    Price=10+i,
                    Quantity=i+100,
                    ProductID = 1000 + i
                });
            }
            object obj = m_service.Save(entity, list);
          
            Assert.IsNotNull(obj);
        }

        [Test]
        public void Update()
        {
            var entity = m_service.Get(1);

            entity.Value[0] = new OrderDetail()
            {
                ProductID = 1000,
                DetailID = 1,
                Price = 13.34M,
                Quantity = 200.1M
            };
            m_service.Update(entity);
        }

        [Test]
        public void Delete()
        {
            var entity = m_service.Get(1);
            m_service.Delete(entity);
        }
    }
}

 

注:测试时需要引用nunit.framework工具