0

在程序集扫描(大约 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() 调用中的其他配置文件?(它们不在要扫描的程序集中)。

4

1 回答 1

0

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

于 2017-01-11T01:39:59.330 回答