zoukankan      html  css  js  c++  java
  • 【AutoMapper基础】简单示例--Flattening

    【Flattening】

    One of the common usages of object-object mapping is to take a complex object model and flatten it to a simpler model. 

    【类图】

    【Code】

    CPU:

    public class CPU
        {
            public string ProducerName { get; set; }
            public string ModelName { get; set; }
            public double MainFrequency { get; set; }
        }
    View Code

    InternalMemory:

    public class InternalMemory
        {
            public string ProducerName { get; set; }
            public string ModelName { get; set; }
            public double Size { get; set; }
            public double Frequency { get; set; }
        }
    View Code

    Monitor:

    public class Monitor
        {
            public string ProducerName { get; set; }
            public string ModelName { get; set; }
            public double Size { get; set; }
        }
    View Code

    Computer:

    public class Computer
        {
            public string Name { get; set; }
            public double Price { get; set; }
            public DateTime ProduceDate { get; set; }
            public CPU CPU { get; set; }
            public InternalMemory InternalMemory { get; set; }
            public Monitor Monitor { get; set; }
        }
    View Code

    Program:

    class Program
        {
            static void Main(string[] args)
            {
                Computer computer = new Computer()
                {
                    Name = "联想Y450",
                    Price = 5199.00,
                    ProduceDate = new DateTime(2009, 10, 10),
                    CPU = new CPU()
                    {
                        MainFrequency = 2.2,
                        ModelName = "Core T6600",
                        ProducerName = "Intel"
                    },
                    InternalMemory = new InternalMemory()
                    {
                        Frequency = 1333,
                        ModelName = "DDR3",
                        ProducerName = "尔必达",
                        Size = 4.0
                    },
                    Monitor = new Monitor()
                    {
                        ProducerName = "LG",
                        Size = 14.0
                    }
                };
    
                AutoMapper.Mapper.CreateMap<Computer, ComputerMainInfo>();
                ComputerMainInfo computerMainInfo = AutoMapper.Mapper.Map<Computer, ComputerMainInfo>(computer);
    
                Console.WriteLine(computerMainInfo.CPUModelName);
                Console.Read();
            }
        }
    View Code

    AutoMapper自动根据名称搜索完成映射,这就从复杂的Computer映射到简化的ComputerMainInfo。

  • 相关阅读:
    八、基本数据结构(图形结构)
    七、基本数据结构(树形结构)
    4、使用 ImportTsv 将 Hive 数据导入 Hbase
    六、跳表
    五、二分法查找
    四、归并排序 && 快速排序
    一、kafka 介绍 && kafka-client
    三、排序之冒泡、插入、选择
    二、YARN
    三、synchronized & lock
  • 原文地址:https://www.cnblogs.com/fb-boy/p/3480602.html
Copyright © 2011-2022 走看看