目录
一, 定义源映射类和被映射类 DTO
二, 注入 AutoMapper
三, 配置映射
四, 调用 AutoMapper 完成赋值
五, 运行测试
一, 定义源映射对象
为了体现 AutoMapper 映射特性, 在 SocialAttribute 中的 Name 属性没有定义在 People 中, People 的 Ear 属性也不存在与 SocialAttribute 和 PhysicalAttribute 中.
代码如下:
- /// <summary>
- /// 身体属性
- /// </summary>
- public class PhysicalAttribute
- {
- public string Eye { get; set; }
- public string Mouth { get; set; }
- }
- PhysicalAttribute
- /// <summary>
- /// 社会属性
- /// </summary>
- public class SocialAttribute
- {
- public int Age { get; set; }
- public bool IsMarried { get; set; }
- public string Name { get; set; }
- }
- SocialAttribute
- public class PeopleDto
- {
- public string Eye { get; set; }
- public string Mouth { get; set; }
- public string Ear { get; set; }
- public int Age { get; set; }
- public bool IsMarried { get; set; }
- }
- PeopleDto
二, 注入 AutoMapper
例子中使用的 IoC 容器是 Autofac, 不使用 Autofac 的话, 仅用 NetCore 框架集成的 IoC 容器也可以实现.
注册 AutoMapper 必要组件:
- public static class AutoMapperInjection
- {
- public static ContainerBuilder LoadAutoMapper(this ContainerBuilder builder)
- {
- builder.RegisterType<MapperConfigurationExpression>().SingleInstance();
- builder.Register(m => {
- var mapperConfigurationExpression = m.Resolve<MapperConfigurationExpression>();
- var instance = new MapperConfiguration(mapperConfigurationExpression);
- return instance;
- });
- builder.Register(m => {
- var mapperConfiguration = m.Resolve<MapperConfiguration>();
- return mapperConfiguration.CreateMapper();
- });
- return builder;
- }
- }
三, 配置映射
ForMember: 映射两个类之间的属性关系.
People 类中的 Ear 属性并不存在于任何映射源类中, 我们可以使用 ForMember(m => m.Ear, n => n.Ignore()) 忽略该属性, 当然也可以不写这段代码, 对 AutoMapper 不会有任何影响, 但是为了后期维护更方便, 我比较习惯将 Dto 类的属性写全.
SocialAttribute 类中的 Name 属性不存在与 People 类中, 直接忽略它, Name 也不会被 AutoMapper 赋值.
- public class AutoMapperProfile: Profile
- {
- public void Mapping(ILifetimeScope scope) {
- var expression = scope.Resolve<MapperConfigurationExpression>();
- expression.CreateMap<PhysicalAttribute, PeopleDto>()
- .ForMember(m => m.Eye, n => n.MapFrom(s => s.Eye))
- .ForMember(m => m.Mouth, n => n.MapFrom(s => s.Mouth));
- //.ForMember(m => m.Ear, n => n.Ignore());
- expression.CreateMap<SocialAttribute, PeopleDto>()
- .ForMember(m => m.Age, n => n.MapFrom(s => s.Age))
- .ForMember(m => m.IsMarried, n => n.MapFrom(s => s.IsMarried));
- }
- }
四, 调用 AutoMapper 完成赋值
调用 AutoMapper, 将 PhysicalAttribute 和 SocialAttribute 的值赋给 PeopleDto
- public class DtoHelper
- {
- private IMapper mapper;
- public DtoHelper(IMapper _mapper) {
- mapper = _mapper;
- }
- public PeopleDto GetDto(PhysicalAttribute physical,SocialAttribute social) {
- PeopleDto peopleDto = new PeopleDto();
- mapper.Map(social, mapper.Map(physical, peopleDto));
- return peopleDto;
- }
- }
五, 运行测试
测试框架使用的 xUnit
- public class DtoHelperTest
- {
- [Fact]
- public void GetDto() {
- //moke
- ContainerBuilder builder = new ContainerBuilder();
- builder.LoadAutoMapper();
- builder.RegisterType<AutoMapperProfile>();
- IContainer Container = builder.Build();
- using (var scope = Container.BeginLifetimeScope())
- {
- scope.Resolve<AutoMapperProfile>().Mapping(scope);
- PeopleDto result = new PeopleDto() { Eye = "双眼皮", Mouth = "红润", Age = 18, IsMarried = false };
- PhysicalAttribute physical = new PhysicalAttribute() { Eye = "双眼皮", Mouth = "红润" };
- SocialAttribute social = new SocialAttribute() { Name = "张三", IsMarried = false, Age = 18 };
- PeopleDto output = new DtoHelper(scope.Resolve<IMapper>()).GetDto(physical, social);
- //Assert.Same(result, output);
- Assert.Equal(JsonConvert.SerializeObject(result), JsonConvert.SerializeObject(output));
- }
- }
- }
通过测试!
参考项目:
来源: https://www.cnblogs.com/fb208/p/11548250.html