1

我想使用 AutoMapper 将 DTO 映射到模型。但是我的项目包含一些用于原始类型默认值的自定义规则,例如 [string null value is "_"]、[default int value is -1] 和 [date time fields must be integer like 19990221] 等。

在我的测试中,我有一个具有 Null 字符串值的外部 DTO。但是我的内部系统模型有一个“空字符串必须表示为”_“字符串”规则(它很烂,但它是一个遗留的设计决策)。

楷模:

public class OuterDTO
{
    public string TestString { get; set; }
}

public class InnerModel
{
    public string TestString { get; set; }
}

Automapper 类型转换器:

public class CustomStringToStringConverter : ITypeConverter<string, string>
{
    public string Convert(ResolutionContext context)
    {
        string source = (string)context.SourceValue;

        if (source == "_")
            return null;

        return source;
    }
}

public class StringToSafirStringConverter : ITypeConverter<string, string>
{
    public string Convert(ResolutionContext context)
    {
        string source = (string)context.SourceValue;

        if (source == null)
            return "_";

        return source;
    }
}

我的测试是:

[TestMethod]
    public void StringToSafirStringConverterAndSafirStringToStringConverter_SafirDefaultNullStringAndNullString_ReturnsNullAndSafirNullString()
    {
        //Arrenge
        Mapper.CreateMap<string, string>().ConvertUsing(new StringToSafirStringConverter());//this has to used be DTO To Model map
        Mapper.CreateMap<string, string>().ConvertUsing(new SafirStringToStringConverter());//this has to used be Model To DTO map
        Mapper.CreateMap<InnerModel, OuterDTO>();
        Mapper.CreateMap<OuterDTO, InnerModel>();
        Mapper.AssertConfigurationIsValid();

        InnerModel expected1 = new InnerModel()
        {
            TestString = null
        };

        OuterDTO inner1 = new OuterDTO()
        {
            TestString = "_"
        };

        OuterDTO expected2 = new OuterDTO()
        {
            TestString = "_"
        };

        InnerModel outer1 = new InnerModel()
        {
            TestString = null
        };

        //Act
        InnerModel actual1 = Mapper.Map<OuterDTO, InnerModel>(inner1);
        OuterDTO actual2 = Mapper.Map<InnerModel, OuterDTO>(outer1);

        //Assert
        Assert.AreEqual(expected1.TestString, actual1.TestString);
        Assert.AreEqual(expected2.TestString, actual2.TestString);
    }

第二个断言失败。

我必须将 DTO 映射到模型和模型到 DTO 的数百个案例,我也有同样的整数问题。我如何选择转换器来进行不同的映射?

4

1 回答 1

1

您正在声明从字符串到字符串的两个映射:

Mapper.CreateMap<string, string>().ConvertUsing(new StringToSafirStringConverter());
Mapper.CreateMap<string, string>().ConvertUsing(new SafirStringToStringConverter());

映射不能被覆盖,因此如果您查看 AutoMapper 配置,您只会看到一个字符串到字符串的映射。(即使您可以定义两个字符串到字符串的映射,AutoMapper 也不知道使用哪一个。)

这样做的方法是使用 ValueResolvers 定义属性的映射:

Mapper.CreateMap<InnerModel, OuterDTO>()
                .ForMember(dest => dest.TestString,
                    opt => opt.ResolveUsing<StringToSafirStringResolver>()
                       .FromMember(source => source.TestString));

Mapper.CreateMap<OuterDTO, InnerModel>()
                .ForMember(dest => dest.TestString,
                    opt => opt.ResolveUsing<SafirStringToStringResolver>()
                      .FromMember(source => source.TestString));

然后你写 ValueResolvers,说:

public class StringToSafirStringResolver : ValueResolver<string, string>
{
    protected override string ResolveCore(string source)
    {
        return source ?? "_";
    }
}

public class SafirStringToStringResolver : ValueResolver<string, string>
{
    protected override string ResolveCore(string source)
    {
        return source == "_" ? null : source;
    }
}

然后,您可以对 int 和 datetime 值执行相同的操作。

于 2014-09-17T16:16:03.150 回答