且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用AutoMapper映射子集合

更新时间:2022-06-24 14:53:25

在创建的地图,你可以使用的方法和该方法几乎可以做到任何东西。例如:

When creating the maps you can use a method and that method can do pretty much anything. For example:

public void MapStuff()
{
    Mapper.CreateMap<StoreDTO, Store>()
        .ForMember(dest => dest.Location, opt => opt.MapFrom(source => DoMyCleverMagic(source)));
}

private ReturnType DoMyCleverMagic(Location source)
{
    //Now you can do what the hell you like. 
    //Make sure to return whatever type is set in the destination
}

使用这种方法,你可以通过它的编号 StoreDTO ,它可以实例化一个位置:)

Using this method you could pass it an Id in the StoreDTO and it can instantiate a location :)