zoukankan      html  css  js  c++  java
  • AutoMapper

        public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime date { set; get; }
        }
        public class StudentDTO
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public string date { set; get; }
        }
        public class StudentActivityDTO
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public string datetime { set; get; }
        }
    创建类
        public class StudentProfile : Profile
        {
            protected override void Configure()
            {
                //Student->StudentDTO
                //CreateMap<Student, StudentDTO>();
                //Student->StudentDTO
                CreateMap<Student, StudentDTO>()
                    .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
                    .ForMember(d => d.Name, opt => opt.MapFrom(s => s.Name))
                    .ForMember(d => d.Age, opt => opt.MapFrom(s => s.Age.ToString() + ""))
                    .ForMember(d => d.date, opt => opt.MapFrom(s => string.Format("{0:yyyy-MM-dd}", s.date)));
    
                //Student->StudentActivityDTO
                CreateMap<Student, StudentActivityDTO>()
                    .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
                    .ForMember(d => d.Name, opt => opt.MapFrom(s => s.Name))
                    .ForMember(d => d.Age, opt => opt.MapFrom(s => s.Age.ToString() + ""))
                    .ForMember(d => d.datetime, opt => opt.MapFrom(s => string.Format("{0:yyyy-MM-dd}", s.date)));
            }
        }
    创建 Profile

    Mapper.CreateMap<Source, Destination>(); //直接依据字段名转换两个类

    Student stu = new Student() { Id = 1, Name = "2", Age = 12, date = DateTime.Now };
    StudentDTO stu_dto = Mapper.Map<StudentDTO>(stu);
  • 相关阅读:
    洛谷P2345 奶牛集会
    洛谷P3531 [POI2012]LIT-Letters
    codevs 4163 hzwer与逆序对
    各种读入方式速度比较
    洛谷P1420 最长连号
    TCPDump:捕获并记录特定协议 / 端口
    linux下抓取网页快照
    Pro Android 4 第五章 理解Intent
    UpdatePanel和jQuery不兼容
    RAC 11.2.0.4 安装 遇到 INS-06001
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/10265348.html
Copyright © 2011-2022 走看看