NetCore+AutoMapper多个对象映射到一个Dto对象

目录

一、定义源映射类和被映射类DTO

二、注入AutoMapper

三、配置映射

四、调用AutoMapper完成赋值

五、运行测试

一、定义源映射对象

为了体现AutoMapper映射特性,在SocialAttribute中的Name属性没有定义在People中,People的Ear属性也不存在与SocialAttribute和PhysicalAttribute中。

NetCore+AutoMapper多个对象映射到一个Dto对象

代码如下:

    /// <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 = , IsMarried = false };
PhysicalAttribute physical = new PhysicalAttribute() { Eye = "双眼皮", Mouth = "红润" };
SocialAttribute social = new SocialAttribute() { Name = "张三", IsMarried = false, Age = };
PeopleDto output = new DtoHelper(scope.Resolve<IMapper>()).GetDto(physical, social);
//Assert.Same(result, output);
Assert.Equal(JsonConvert.SerializeObject(result), JsonConvert.SerializeObject(output));
} }
}

通过测试!

NetCore+AutoMapper多个对象映射到一个Dto对象

参考项目:https://github.com/FB208/CodeSpace/tree/master/CodeSpace.CSharp/WebMvc/DemoClass/AutoMapperDemo

上一篇:SQLServer获取临时表所有列名或是否存在指定列名的方法


下一篇:Java服务端单元测试指南