自动处理配置文件的多个源

时间:2017-01-10 04:15:34

标签: c# automapper automapper-5

在组装扫描之前(大约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()调用的一部分? (它们不在要扫描的组件中。)

1 个答案:

答案 0 :(得分:0)

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