AutoMapper的使用

当前使用版本8.1.1

最新版本9.0.0 静态方法被去掉了

配置

public class AutoMapperConfig
{
	public static void Init()
	{
		MapperConfigurationExpression config = new MapperConfigurationExpression();
        //显示转换
		config.CreateMap()
			.ForMember(dest => dest.code,opt => opt.MapFrom(src=>src.F_EnCode))
			.ForMember(dest => dest.parent, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.F_ParentId)?"0": src.F_ParentId))
			.ForMember(dest => dest.level, opt => opt.MapFrom(src => src.F_Layers))
			.ForMember(dest => dest.name, opt => opt.MapFrom(src => src.F_FullName))
			.ForMember(dest => dest.shortName, opt => opt.MapFrom(src => src.F_SimpleSpelling))
			.ForMember(dest => dest.sortIdx, opt => opt.MapFrom(src => src.F_SortCode ?? 0))
			.ForMember(dest => dest.desc, opt => opt.MapFrom(src => src.F_Description));
        //自动映射字段
		config.CreateMap().ReverseMap();
		AutoMapper.Mapper.Initialize(config);
	}
}

扩展

/// 
/// 实体映射
/// 
/// 源对象
/// 目标对象
/// 源对象
/// 
public static TDest Map(this TSource source)
{
	return AutoMapper.Mapper.Map(source);
}

测试

//转换实体
string creator = Guid.NewGuid().ToString();
List areas = new List() { new SysAreaEntity() {
		F_ParentId = "",
		F_Layers = 1,
		F_EnCode = "110000",
		F_FullName = "北京",
		F_SimpleSpelling = "bei jing",
		F_SortCode = 1,
		F_EnabledMark = true,
		F_CreatorTime = new DateTime(2001,10,12),
		F_CreatorUserId = creator
	},
	new SysAreaEntity() {
		F_ParentId = "110000",
		F_Layers = 2,
		F_EnCode = "110100",
		F_FullName = "东城区",
		F_SimpleSpelling = "dong cheng qu",
		F_SortCode = null,
		F_EnabledMark = true,
		F_CreatorTime = new DateTime(2001,10,12),
		F_CreatorUserId = creator
	}
};

List list = areas.Select(a => a.Map()).ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(list));

输出:

[
	{
		"code": "110000",
		"parent": "0",
		"level": 1,
		"name": "北京",
		"shortName": "bei jing",
		"sortIdx": 1,
		"desc": null
	},
	{
		"code": "110100",
		"parent": "110000",
		"level": 2,
		"name": "东城区",
		"shortName": "dong cheng qu",
		"sortIdx": 0,
		"desc": null
	}
]

自动映射测试

//自动映射
List entities = new List() {
	new SimpleEntity{ 
		Name = "购物车",
		Id = 1,
		Address = "浙江杭州西湖区",
		Details = new List{ 
			new SimpleDetail{ 
				Name = "苹果",
				Number = 10,
				Price = 5.8m
			},
			new SimpleDetail{
				Name = "梨",
				Number = 5,
				Price = 7.9m
			}
		}
	}
};

Console.WriteLine("正向 自动映射");
List model = entities.Select(e => e.Map()).ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(model));

//反转
Console.WriteLine("反向 自动映射");
entities = model.Select(e => e.Map()).ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(entities));

输出:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

明细类没有配置映射

config.CreateMap().ReverseMap();

正常输出

正向 自动映射
[
	{
		"name": "购物车",
		"id": "1",
		"address": "浙江杭州西湖区",
		"details": [
			{
				"name": "苹果",
				"number": 10,
				"price": 5.8
			},
			{
				"name": "梨",
				"number": 5,
				"price": 7.9
			}
		]
	}
]
反向 自动映射
[
	{
		"Name": "购物车",
		"Id": 1,
		"Address": "浙江杭州西湖区",
		"Details": [
			{
				"Name": "苹果",
				"Number": 10,
				"Price": 5.8
			},
			{
				"Name": "梨",
				"Number": 5,
				"Price": 7.9
			}
		]
	}
]


 

你可能感兴趣的:(C#)