首页 > 解决方案 > Automapper 常用地图部分

问题描述

即我有以下地图类:

public class AutoMapperAddress : AutoMapper.Profile
{
    public AutoMapperAddress()
    {
        CreateMap<AddressDto, ViewModels.AddressElementAPI>();
    }
}

那么我应该在其他地方使用它,即:

public class AutoMapperLocation : AutoMapper.Profile
{
    public AutoMapperLocation()
    {
        // location mappers here
        // I need the same Address mapping as AutoMapperAddress has
    }
}

public class AutoMapperCarrier : AutoMapper.Profile
{
    public AutoMapperCarrier ()
    {
        // carrier mappers here
        // I need the same Address mapping as AutoMapperAddress has
    }
}

怎么做?

添加:

我试图在创建映射器对象期间解决它:

        mapper = (new MapperConfiguration(cfg => {
            cfg.AddProfile<Models.AutoMapperRules.AutoMapperLocation>();
            cfg.AddProfile<Models.AutoMapperRules.AutoMapperAddress>();
            }
        )).CreateMapper();

但我想将它封装在 AutoMapperLocation 中,因为没有 AutoMapperAddress 就没有 AutoMapperLocation

标签: c#mappingautomapperautomapper-6

解决方案


推荐阅读