您的位置:首页 > 移动开发

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

2017-03-29 22:24 134 查看
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; }
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐