自动映射配置文件的多个源



在装配扫描(大约 4.x)之前,我们做了如下操作,以提供一种集中的方法来添加配置文件:

/// <summary>
///     Class that allows all the AutoMapper profiles to be initiated for all assemblies within the platform.
/// </summary>
public class OzCpAutoMapperConfig
{
    private Type[] _ExternalProfiles;
    /// <summary>
    ///     For the 201610.02 release we are not using assembly scanning so we get the caller to pass
    ///     in the profiles they want us to register.
    /// </summary>
    /// <param name="aExternalProfiles">List of automapper profile classes that we also need to register</param>
    public void ConfigurationApproachFor201610_02(Type[] aExternalProfiles)
    {
        _ExternalProfiles = aExternalProfiles;
        Mapper.Initialize(DoMapperConfigFor201610_02);
    }
    /// <summary>
    ///     Internal helper method that populates the AutoMapper configuration object.
    /// </summary>
    /// <param name="aMapperConfiguration">Instance of AutoMapper configuration object that we need to use.</param>
    private void DoMapperConfigFor201610_02(IMapperConfiguration aMapperConfiguration)
    {
        //Add all the external mapper configurations passed in by the original caller
        foreach (Type externalProfile in _ExternalProfiles)
        {
            aMapperConfiguration.AddProfile(Activator.CreateInstance(externalProfile) as Profile);
        }
        //Now add all the platform profiles
        aMapperConfiguration.AddProfile<CarnivalApiServiceAutoMapperProfile>();
        aMapperConfiguration.AddProfile<CarnivalOpenseasApiServiceAutoMapperProfile>();
        ...
   }
}

所以现在在 5.2 中,我们希望使用程序集扫描并能够传入一些额外的配置文件进行注册。但是,IMapperConfiguration 不再可用。因此,虽然我可以做到:

Mapper.Initialize(cfg => { cfg.AddProfiles("MyNameSpace.Application", "MyNameSpace.Core", ....); 

如何添加要包含在 Mapper.Initialize() 调用中的其他配置文件?(它们不在要扫描的程序集中)。

IMapperConfiguration 在 5.2 中更名为 IMapperConfigurationExpression。因此,在使用重命名的接口后,可以使用上述相同的方法。

相关内容

  • 没有找到相关文章

最新更新