使用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; }
}
}
}