2

我找到了一种基于活动文化使用 Automapper 进行语言映射的方法。

问题是是否可以构建一个通用解析器来映射所有使用解析器的模型。

在这种情况下,要映射的模型始终具有相同的属性 Id 和 Name(包括语言属性 Name_PT、Name_FR 和 Name_EN):

// 楷模

public class MakeDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class MakeViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ModelDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class ModelViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

public class FuelDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

public class FuelViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

// 自动映射器配置文件

public class DtoToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "DtoToViewModelMappings"; }
    }

    protected override void Configure()
    {
        CreateMaps();
    }

    private static void CreateMaps()
    {
        Mapper.CreateMap<ModelDto, ModelViewModel>();
        Mapper.CreateMap<MakeDto, MakeViewModel>()
            .ForMember(dest => dest.Name, opt => opt.ResolveUsing<CultureResolver>());
        Mapper.CreateMap<FuelDto, FuelViewModel>();
    }

    public class CultureResolver : ValueResolver<MakeDto, string>
    {
        protected override string ResolveCore(MakeDto makeDto)
        {
            switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
            {
                case "FR":
                    return makeDto.Name_FR;
                case "EN":
                    return makeDto.Name_EN;
            }
            return makeDto.Name_PT;
        }   
    }
}

谢谢。

4

1 回答 1

2

您可以提取如下界面:

public interface ILocalizable
{
    string Name { get; set; }
    string Name_PT { get; set; }
    string Name_FR { get; set; }
    string Name_EN { get; set; }
}

public class FuelDto : ILocalizable
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Name_PT { get; set; }
    public string Name_FR { get; set; }
    public string Name_EN { get; set; }
}

然后调整您的解析器,如下所示:

public class CultureResolver : ValueResolver<ILocalizable, string>
{
    protected override string ResolveCore(ILocalizable dto)
    {
        switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
        {
            case "FR":
                return dto.Name_FR;
            case "EN":
                return dto.Name_EN;
        }
        return dto.Name_PT;
    }   
}
于 2012-10-27T07:15:40.333 回答