Automapper:将 X 类型的 属性 从源对象映射到目标对象,具有等值属性到 X 类型
Automapper: Map property of type X from source object to destination object with equivlanet properties to type X
不确定我的措辞是否正确,希望示例足够清楚。
我想做的事情对我来说似乎很基础,所以我假设我遗漏了一些明显的东西。
对于此示例,两个 ForMember
映射很简单,可以完成工作。问题是对于更复杂的 class,如果配置了任何中间映射,您如何简单地将一个对象的 属性 映射到整个目标?
我搜索了一段时间,最接近找到答案的是 here 但 ConvertUsing
语法对我不起作用(我使用的是 Automapper 4.2.1)
这里是例子 classes:
public class UserRoleDto
{
public string Name { get; set; }
public string Description { get; set; }
}
public class DbRole
{
public Guid RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class DbUserRole
{
public Guid UserId { get; set; }
public DbRole Role { get; set; }
}
这是我的 Automapper 配置设置测试用例(在 LINQPad 中测试,这就是最后一行末尾的 Dump() 的用途)
var dbRole = new DbRole { RoleId = Guid.NewGuid(), Name = "Role Name", Description = "Role Description" };
var dbUserRole = new DbUserRole { UserId = Guid.NewGuid(), Role = dbRole };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DbRole, UserRoleDto>();
/* Works but verbose for a class with more than a few props */
cfg.CreateMap<DbUserRole, UserRoleDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Role.Name))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Role.Description))
;
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole).Dump();
传入要映射的子对象如何?例如
cfg.CreateMap<DbRole, UserRoleDto>();
然后,您将映射 dbUserRole.Role
,而不是映射 dbUserRole
。
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole.Role);
这是另一个使用以下内容的类似示例 类:
public class Person
{
public int person_id;
public int age;
public string name;
}
public class Address
{
public int address_id;
public string line1;
public string line2;
public string city;
public string state;
public string country;
public string zip;
}
public class PersonWithAddress
{
public int person_id;
public int age;
public string name;
public InnerAddress address;
}
public class InnerAddress
{
public string city;
public string state;
public string country;
}
使用以下测试用例:
var person = new Person { person_id = 100, age = 30, name = "Fred Flintstone" };
var address = new Address { address_id = 500, line1 = "123 Main St", line2 = "Suite 3", city = "Bedrock", state = "XY", country = "GBR", zip="90210" };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Person, PersonWithAddress>();
cfg.CreateMap<Address, InnerAddress>();
});
var mapper = config.CreateMapper();
var person_with_address = mapper.Map<Person, PersonWithAddress>(person);
person_with_address.address = new InnerAddress();
mapper.Map<Address, InnerAddress>(address, person_with_address.address);
此致,
罗斯
不确定我的措辞是否正确,希望示例足够清楚。
我想做的事情对我来说似乎很基础,所以我假设我遗漏了一些明显的东西。
对于此示例,两个 ForMember
映射很简单,可以完成工作。问题是对于更复杂的 class,如果配置了任何中间映射,您如何简单地将一个对象的 属性 映射到整个目标?
我搜索了一段时间,最接近找到答案的是 here 但 ConvertUsing
语法对我不起作用(我使用的是 Automapper 4.2.1)
这里是例子 classes:
public class UserRoleDto
{
public string Name { get; set; }
public string Description { get; set; }
}
public class DbRole
{
public Guid RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class DbUserRole
{
public Guid UserId { get; set; }
public DbRole Role { get; set; }
}
这是我的 Automapper 配置设置测试用例(在 LINQPad 中测试,这就是最后一行末尾的 Dump() 的用途)
var dbRole = new DbRole { RoleId = Guid.NewGuid(), Name = "Role Name", Description = "Role Description" };
var dbUserRole = new DbUserRole { UserId = Guid.NewGuid(), Role = dbRole };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DbRole, UserRoleDto>();
/* Works but verbose for a class with more than a few props */
cfg.CreateMap<DbUserRole, UserRoleDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Role.Name))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Role.Description))
;
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole).Dump();
传入要映射的子对象如何?例如
cfg.CreateMap<DbRole, UserRoleDto>();
然后,您将映射 dbUserRole.Role
,而不是映射 dbUserRole
。
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole.Role);
这是另一个使用以下内容的类似示例 类:
public class Person
{
public int person_id;
public int age;
public string name;
}
public class Address
{
public int address_id;
public string line1;
public string line2;
public string city;
public string state;
public string country;
public string zip;
}
public class PersonWithAddress
{
public int person_id;
public int age;
public string name;
public InnerAddress address;
}
public class InnerAddress
{
public string city;
public string state;
public string country;
}
使用以下测试用例:
var person = new Person { person_id = 100, age = 30, name = "Fred Flintstone" };
var address = new Address { address_id = 500, line1 = "123 Main St", line2 = "Suite 3", city = "Bedrock", state = "XY", country = "GBR", zip="90210" };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Person, PersonWithAddress>();
cfg.CreateMap<Address, InnerAddress>();
});
var mapper = config.CreateMapper();
var person_with_address = mapper.Map<Person, PersonWithAddress>(person);
person_with_address.address = new InnerAddress();
mapper.Map<Address, InnerAddress>(address, person_with_address.address);
此致,
罗斯