|
什么是AutoMapper?
AutoMapper是一个对象和对象间的映射器。对象与对象的映射是通过转变一种类型的输入对象为一种不同类型的输出对象工作的。让AutoMapper有意思的地方在于它提供了一些将类型A映射到类型B这种无聊的事情的有趣惯例。只要类型B遵守AutoMapper已经建立的惯例,大多数情况下,映射两种类型零配置就可以了。
为什么使用AutoMapper?
映射代码是无聊的。测试映射代码更无聊。AutoMapper提供了一些简单配置,还有一些简单的映射测试。真正的问题可能是“为什么使用对象-对象的映射呢”?映射可能发生在一个应用的许多地方,但大多数情况下都发生在层与层之间的边界,比如UI/Domain层之间,或者Service/Domain层之间。关注一层通常和关注另一层发生冲突,因此对象-对象间的映射来隔离模型model,这样就只会影响每一层关注的类型。
我自己写的一个例子,例子中有:
1:int和string的转换
2:int?和int的转换
3:枚举和string的转换
4:对象和string的转换
等等
源对象:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AutoMapperDemo
- {
- public class Domain
- {
- /// <summary>
- /// 姓名
- /// </summary>
- public String Name { get; set; }
- /// <summary>
- /// 年龄
- /// </summary>
- public int Age { get; set; }
- /// <summary>
- /// 身高
- /// </summary>
- public int Stature { get; set; }
- /// <summary>
- /// 体重
- /// </summary>
- public int? Weight { get; set; }
- /// <summary>
- /// 性别
- /// </summary>
- public Sex Sex { get; set; }
- /// <summary>
- /// 车辆信息
- /// </summary>
- public Car Car { get; set; }
- /// <summary>
- /// 个人网站
- /// </summary>
- public String Url { get; set; }
-
- }
- public enum Sex
- {
- 男,女
- }
- public class Car
- {
- /// <summary>
- /// 车牌
- /// </summary>
- public String VehicleNo { get; set; }
- /// <summary>
- /// 车牌颜色
- /// </summary>
- public String PlateColor { get; set; }
- /// <summary>
- /// 核定载客位
- /// </summary>
- public int Seats { get; set; }
- }
- }
复制代码
目标对象:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AutoMapperDemo
- {
- public class WebModel
- {
- public String Name { get; set; }
- public int age { get; set; }
- public String stature { get; set; }
- public int weight { get; set; }
- public String Sex { get; set; }
- public String Car { get; set; }
- public string AnotherValue { get; set; }
- public String Site { get; set; }
- }
- }
复制代码
转换代码如下:
- using AutoMapper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AutoMapperDemo
- {
- class Program
- {
- //In 4.2.1 version of AutoMapper and later, AutoMapper provides two APIs: a static and an instance API.The static API:
- //Mapper.Initialize(cfg => {
- //cfg.AddProfile<AppProfile>();
- //cfg.CreateMap<Source, Dest>();
- //});
- //var dest = Mapper.Map<Source, Dest>(new Source());
- //And the instance API:
- //var config = new MapperConfiguration(cfg => {
- //cfg.AddProfile<AppProfile>();
- //cfg.CreateMap<Source, Dest>();
- //});
- //var mapper = config.CreateMapper();
- //// or
- //IMapper mapper = new Mapper(config);
- //var dest = mapper.Map<Source, Dest>(new Source());
- static void Main(string[] args)
- {
- Mapper.Initialize(x => x.AddProfile<SourceProfile>());
- //Mapper.Initialize(cfg => cfg.CreateMap<Order, OrderDto>());
- Domain model = new Domain()
- {
- Name = "架构师",
- Age = 3,
- Stature = 175,
- Sex = Sex.男,
- Car = new Car()
- {
- VehicleNo = "沪A8888",
- PlateColor = "黄色",
- Seats = 6
- },
- Url = "http://www.itsvse.com"
- };
- Mapper.CreateMap<Domain, WebModel>().ForMember(x => x.Site, y => { y.MapFrom(src => src.Url); });
- //int转string
- Mapper.CreateMap<int, String>().ConvertUsing((intNum, context) => intNum.SourceValue.ToString());
- //string转int
- Mapper.CreateMap<string, int>().ConvertUsing((context, intNum) => Convert.ToInt32(context.SourceValue));
- var entity = Mapper.Map<Domain, WebModel>(model);
- //简写
- //var entity = Mapper.Map<WebModel>(model);
- //验证目标属性是否都被映射,如果存在未被映射的属性,则抛出异常。
- //Mapper.AssertConfigurationIsValid();
- Console.WriteLine(entity.Name);
- Console.WriteLine(entity.age);
- Console.WriteLine(entity.stature);
- Console.WriteLine(entity.weight);
- Console.WriteLine(entity.Sex);
- Console.WriteLine(entity.Car);
- Console.WriteLine(entity.Site);
- Console.ReadKey();
- }
- /// <summary>
- /// 指定对象转int
- /// </summary>
- public class CarTypeConverter : ITypeConverter<Car, String>
- {
- public string Convert(ResolutionContext context)
- {
- return (context.SourceValue as Car) == null ? null : (context.SourceValue as Car).VehicleNo;
- }
- }
- public class SourceProfile : Profile
- {
- protected override void Configure()
- {
- //自定义类型转换
- Mapper.CreateMap<Car, String>().ConvertUsing<CarTypeConverter>();
- }
- }
- }
- }
复制代码
源码下载:
|
上一篇:AutoMapper 类型转换报错解决办法下一篇:IIS 7 应用程序池自动回收关闭的解决方案
|