zoukankan      html  css  js  c++  java
  • AutoMapper之集合和数组映射

    9.集合和数组映射

    在项目中,集合和数组使用的很多的,继续下来就讲讲他们的映射,很简单。

    /// <summary>
    /// 源对象
    /// </summary>
    public class Source
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }
    
    /// <summary>
    /// 目标对象
    /// </summary>
    public class Destination
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }
    
    /// <summary>
    /// 集合和数组映射测试类
    /// </summary>
    [TestClass]
    public class ListAndArrayMaping
    {
        [TestMethod]
        public void ListMapingTest()
        {
            //初始化映射 和单个对象的映射一样
            Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
    
            var srcList = new List<Source> {
                new Source { Value = 5,Text="Five" }                
            };
    
            
    	//在这里指定类型参数,拿第一个为例;源类型:List<Source>;目标类型:IEnumerable<Destination>;
    	// List映射到IEnumerable;
            IEnumerable<Destination> ienumerableDest1 = Mapper.Map<List<Source>, IEnumerable<Destination>>(srcList);
    	// List映射到ICollection;
            ICollection<Destination> icollectionDest1 = Mapper.Map<List<Source>, ICollection<Destination>>(srcList);
            // List映射到IList;
    	IList<Destination> ilistDest1 = Mapper.Map<List<Source>, IList<Destination>>(srcList);
    	// List映射到List;
            List<Destination> listDest1 = Mapper.Map<List<Source>, List<Destination>>(srcList);
    	// List映射到Array;
            Destination[] arrayDest1 = Mapper.Map<List<Source>, Destination[]>(srcList);
    
            //验证List映射到IEnumerable的结果
            foreach (var m in ienumerableDest1)
            {
                Assert.AreEqual("Five", m.Text);//通过
                Assert.AreEqual(5, m.Value); //通过
            }
            //验证List映射到List结果
            foreach (var m in listDest1)
            {
                Assert.AreEqual("Five", m.Text); //通过
                Assert.AreEqual(5, m.Value); //通过
            }
    
        }
    }
    

    AutoMapper还支持以下集合类型的映射:

    • IEnumerable
    • IEnumerable
    • ICollection
    • ICollection
    • IList
    • IList
    • List
    • Arrays

    以后在项目中使用起来就更加方便了!!!

    出处:https://www.cnblogs.com/wuyunblog/p/6666485.html

    =======================================================================================

    AutoMapper目录

    出处:https://www.cnblogs.com/wuyunblog/tag/AutoMapper/

    您的资助是我最大的动力!
    金额随意,欢迎来赏!
    款后有任何问题请给我留言。

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我。(●'◡'●)

    如果你觉得本篇文章对你有所帮助,请给予我更多的鼓励,求打             付款后有任何问题请给我留言!!!

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【Jack_孟】!

  • 相关阅读:
    Java程序员必会的工具库,代码量减少90%
    Git常用操作
    Eclipse开发环境配置
    Spring Cloud Alibaba Nacos 在Windows10下启动
    MySQL连接异常Communications link failure
    Spring Cloud Alibaba Nacos2.0踩坑
    Spring Cloud Alibaba Nacos配置中心与服务发现
    RocketMQ学习笔记
    Linux开发环境配置
    Clumper尝鲜
  • 原文地址:https://www.cnblogs.com/mq0036/p/15241959.html
Copyright © 2011-2022 走看看