这是AutoMapper ver3.2.1
通过NuGet Package Manager Console
安装的。我尝试在正式的单元测试演示中实现继承映射,但实际上它不起作用。
我的代码如下:
public abstract class ModelBase
{
public string Guid { get; set; }
public DateTime? UpdateDate { get; set; }
}
public class Model : ModelBase
{
public string Name { get; set; }
}
public abstract class ModelBaseDto
{
public string Guid { get; set; }
public DateTime UpdateDate { get; set; }
public bool UpdateDateSpecified { get; set; }
}
public class ModelDto : ModelBaseDto
{
public string Name { get; set; }
}
Mapping configuration
:
AutoMapper.Mapper.CreateMap<ModelBaseDto, ModelBase>()
.ForMember(dest => dest.UpdateDate, opt => opt.Condition(src => src.UpdateDateSpecified))
.ForMember(dest => dest.Guid, opt => opt.Condition(src => !string.IsNullOrWhiteSpace(src.Guid)))
.Include<ModelDto, Model>();
AutoMapper.Mapper.CreateMap<ModelDto, Model>();
最后,还有单元测试代码段:
public void TestMethod1()
{
var model = new Model()
{
Guid = "123"
};
mapping.Map<ModelDto, Model>(
new ModelDto()
{
UpdateDate = new DateTime(),
UpdateDateSpecified = false,
Guid = " ",
Name = "Dto"
}, model);
Assert.IsFalse(model.UpdateDate.HasValue);
Assert.AreEqual(model.Guid, "123");
Assert.AreEqual(model.Name, "Dto", "failed");
}
你会看到第一和第二个断言总是失败。 如果我明确地将条件映射添加到派生类而不是基类,则映射将按预期运行。
有人可以帮忙吗?提前谢谢!