我想使用 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 的数百个案例,我也有同样的整数问题。我如何选择转换器来进行不同的映射?