Automapper - 多对象源和一个目标

时间:2013-10-23 14:11:09

标签: c# asp.net-mvc automapper

我使用自动映射器映射多个对象(db类到ui对象)。

地图1:

Mapper.CreateMap<sourceone, destination>().ForMember(sss => sss.one, m => m.MapFrom(source => source.abc));

地图2:

Mapper.CreateMap<sourcetwo, destination>().ForMember(sss => sss.two, m => m.MapFrom(source => source.xyz));

destination d = new destination();

// Map 1

d = AutoMapper.Mapper.Map<sourceone, destination>(sourceone);

// Map 2

d = AutoMapper.Mapper.Map<sourcetwo, destination>(sourcetwo);

一旦我调用'Map 2',使用Map 1填充的值就会丢失..(即destination.one变空)。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:71)

Map有一个带有源和目标对象的重载:

d = AutoMapper.Mapper.Map<sourceone, destination>(sourceone);

/* Pass the created destination to the second map call: */
AutoMapper.Mapper.Map<sourcetwo, destination>(sourcetwo, d);

答案 1 :(得分:7)

public class Person
{
    public string Name { get; set; }
    public string PhNo { get; set; }
}
public class Company
{
    public int EmpNo { get; set; }
    public string Title { get; set; }
}

public class PersonCompany
{
    public string Name { get; set; }
    public string PhNo { get; set; }

    public int EmpNo { get; set; }
    public string Title { get; set; }
}

//you can test as below
        var pMap = Mapper.CreateMap<Person,PersonCompany>();
        pMap.ForAllMembers(d => d.Ignore()); 
        pMap.ForMember(d => d.Name, opt => opt.MapFrom(s => s.Name))
            .ForMember(d => d.PhNo, opt => opt.MapFrom(s => s.PhNo));

        var cMap = Mapper.CreateMap<Company, PersonCompany>();
        cMap.ForAllMembers(d => d.Ignore());
        cMap.ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
            .ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title));


        var person = new Person { Name = "PersonName", PhNo = "212-000-0000" };
        var personCompany = Mapper.Map<Person,PersonCompany>(person);
        var company = new Company { Title = "Associate Director", EmpNo = 10001 };
        personCompany = Mapper.Map(company, personCompany);

        Console.WriteLine("personCompany.Name={0}", personCompany.Name);
        Console.WriteLine("personCompany.PhNo={0}", personCompany.PhNo);
        Console.WriteLine("personCompany.EmpNo={0}", personCompany.EmpNo);
        Console.WriteLine("personCompany.Title={0}", personCompany.Title);

答案 2 :(得分:2)

如今看起来像这样:

DestinationDto = _mapper.Map(source2, _mapper.Map<source1type, destinationType>(source1));

答案 3 :(得分:0)

根据我的看法,您应该避免像接受的答案中所述那样,使用目标对象的实例调用重载Map方法。这不会让您测试/验证映射配置(Mapper.Configuration.AssertConfigurationIsValid())或这样做,您将在映射中添加很多“忽略”。

一个非常简单的解决方案是创建一个包含源引用的复合类型,并基于该复合类型定义到目标的映射。

类似的东西:

    public class SourceOneTwo
    {
        public SourceOne SourceOne { get; set; }
        public SourceTwo SourceTwo { get; set; }
    }
    static void Main(string[] args)
    {
        var config = new MapperConfiguration(cfg => 
            cfg.CreateMap<SourceOneTwo, Destination>()
            .ForMember(dest => dest.one, m => m.MapFrom(source => source.SourceOne.abc))
            .ForMember(dest => dest.two, m => m.MapFrom(source => source.SourceTwo.xyz)));
        config.AssertConfigurationIsValid();
    }