• 使用AutoMapper 处理DTO数据对象的转换


    using AutoMapper;
    using System;

    namespace DTOtEST
    {
    class Program
    {
    static void Main(string[] args)
    {
    Student s = new Student();
    s.StuID = 123456;
    s.Name = "哈哈哈哈";
    s.Age = 20;

    var dd = new MapperConfiguration(cfg=>cfg.CreateMap<Student,StudentDTO>());
    StudentDTO ss = dd.CreateMapper().Map<StudentDTO>(s);
    Console.WriteLine(ss.Age);
    Console.WriteLine(ss.Name);
    Console.WriteLine(ss.StuID);
    Console.Read();
    }
    }

    //public class MyProfile : Profile
    //{
    // protected override void Configure()
    // {
    // CreateMap<Student, StudentDTO>();
    // CreateMap<Student, StudentDTO>().ForMember(dt => dt.Name, opt => opt.MapFrom(src => src.Name))
    // .ForMember(dt => dt.Age, opt => opt.MapFrom(src => src.Age));
    // }
    //}

    public class Student
    {
    private long stuID;
    public long StuID
    {
    get { return stuID; }
    set { stuID = value; }
    }

    private string name;
    public string Name
    {
    get { return name; }
    set { name = value; }
    }

    private int age;
    public int Age
    {
    get { return age; }
    set { age = value; }
    }

    private string address;

    public string Address
    {
    get { return address; }
    set { address = value; }
    }

    }

    public class StudentDTO
    {
    private long stuID;
    public long StuID
    {
    get { return stuID; }
    set { stuID = value; }
    }

    private string name;
    public string Name
    {
    get { return name; }
    set { name = value; }
    }

    private int age;
    public int Age
    {
    get { return age; }
    set { age = value; }
    }

    }
    }

  • 相关阅读:
    DHCP Option 60 的理解
    程序中的魔鬼数字
    开源GUI-Microwindows之程序入口分析
    http报错之return error code:401 unauthorized
    内存泄漏以及常见的解决方法
    怎样对ListView的项进行排序
    getline函数
    JavaFx初探
    ListBox控件的操作与实现
    SQLite的SQL语法
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/6642154.html
Copyright © 2020-2023  润新知