简体   繁体   English

自动映射器:将类型X的属性从源对象映射到目标对象,并将等值属性映射为类型X

[英]Automapper: Map property of type X from source object to destination object with equivlanet properties to type X

Not sure that I'm wording this the right way so hopefully the example is clear enough. 不确定我用的是正确的措词,因此希望示例足够清楚。

What I'm trying to do seems pretty basic to me so I'm assuming I'm missing something obvious. 我想做的事情对我来说似乎很基础,所以我假设我缺少明显的东西。

For this example, the two ForMember mappings are trivial and get the job done. 对于此示例,这两个ForMember映射很简单,可以完成工作。 The question is for a more complex class, given any intermediate mappings are configured, how do you simply map the property of one object to the entire destination? 问题是对于更复杂的类,给定配置了任何中间映射,您如何简单地将一个对象的属性映射到整个目标?

I searched for a while now and the closest I came to finding an answer is here but the ConvertUsing syntax doesn't work for me (I'm using Automapper 4.2.1) 我现在搜索了一段时间,最接近找到答案的位置在这里,但是ConvertUsing语法对我不起作用(我使用的是Automapper 4.2.1)

Here are the example 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; }
}

And here's my test case with the Automapper config setup (testing in LINQPad, that's what the Dump() is for at the end of the last line) 这是我使用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();

How about passing in the sub-object to be mapped? 传入要映射的子对象怎么样? Eg 例如

cfg.CreateMap<DbRole, UserRoleDto>();

Then, instead of mapping dbUserRole , you would map dbUserRole.Role . 然后,不映射dbUserRole ,而是映射dbUserRole.Role

var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole.Role);

Here is another similar example using the following classes: 这是另一个使用以下类的示例:

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;
}

With the following test case: 使用以下测试用例:

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);

Regards, 问候,

Ross 罗斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AutoMapper将对象映射到任何继承的目标类型 - AutoMapper map object to any inherited destination type A类型的自动映射器将源属性映射到目标列表 - Automapper map source property of type A to destination List<B> Automapper - 如何从源子对象映射到目标 - Automapper - How to map from source child object to destination Automapper如果source为null,则设置目标对象属性 - Automapper If source is null, set destination object properties 如何使用自动映射器将对象映射到未知的目标类型? - How can I use Automapper to map an object to an unknown destination type? 如何使用automapper将源类型的Parent映射到目标类型的集合? - How to use automapper to map Parent of source type into a collection of destination type? 自动映射器,将Source映射到Destination的属性 - Automapper, map Source to property of Destination 如何使用AutoMapper将目标对象与源对象中的子对象进行映射? - How to use AutoMapper to map destination object with a child object in the source object? Automapper 创建目标类型 object,其中没有任何值 - Automapper create destination type object with no values inside 使用 AutoMapper Map function 映射对象时,将源 object 的属性保留到目标属性 - Keep property of source object to destination property when mapping objects using AutoMapper Map function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM