Esempio n. 1
0
        public StudentDto ModleMapper1()
        {
            Cource cource = new Cource();

            cource.ID         = "1";
            cource.CourceName = "数学";
            cource.Grade      = 80.56;

            Student stu = new Student();

            stu.AGE    = 25;
            stu.ID     = "1";
            stu.CID    = "321281199505290919";
            stu.NAME   = "陈兆杰";
            stu.Cource = cource;
            StudentDto stuDto = new StudentDto();

            {
                TypeAdapterConfig config = new TypeAdapterConfig();
                //建立映射关系一, NewConfig 删除任何现有配置
                {
                    config.NewConfig <Student, StudentDto>()
                    .Map(dto => dto.ID, d => d.ID).Map(dto => dto.NAME, d => d.NAME).Map(dto => dto.CourceName, s => s.Cource.CourceName);
                }
                //建立映射关系二,而 ForType 创建或增强配置。
                {
                    //        config.ForType<Student, StudentDto>()
                    //.Map(dto => dto.ID, d => d.ID).Map(dto => dto.NAME, d => d.NAME).Map(dto => dto.CourceName, s => s.Cource.CourceName);
                }
                stuDto = stu.Adapt <StudentDto>(config);//根据config配置,映射stu实体为StudentDto类型
            }
            return(stuDto);
        }