使用AutoMapper 处理DTO数据对象的转换
2017-03-29 22:24
507 查看
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; }
}
}
}
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; }
}
}
}
相关文章推荐
- 使用eval将json字符串转换为json对象并且处理json数据
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
- 报错:对象必须实现 IConvertible;以分隔符进行分割链接concat_ws的使用方法;mysql数据类型转换cast,convert
- javascript数据(对象字符串等)转换成json数据的javascript扩展包的使用
- 使用 ibatis 处理复杂对象数据关系的实例
- 使用 ibatis 处理复杂对象数据关系的实例
- 【第15篇】通过FastJson把字符串转换成JSON和Map和List对象处理json数据
- JNI 对象处理和数据类型转换
- 将指定excel文件中的数据转换成datatable对象,供应用程序进一步处理
- 使用AutoMapper实现Dto和Model的自由转换(中)
- 使用Javascript/jQuery将javascript对象转换为json格式数据
- 使用stringstream对象实现数据类型之间的转换
- java对象和json数据转换实现方式3-使用jackson实现
- 【第21篇】通过Gson去把字符串转成json,对象转成Json,数组转换成Json处理Json数据
- 贫血模型;DTO:数据传输对象(Data Transfer Object);AutoMapper ;Domain Model(领域模型);DDD(领域驱动设计)
- OC中Foundation集合的使用(其中包括基本数据类型与对象进行转换)
- 使用Java 语言可以方便的把任意对象转换成json格式的数据
- 使用 ibatis 处理复杂对象数据关系的实例
- iOS中使用KVC实现JSON数据与Objective-C实体对象之间的转换