0
我期望目標將使用ConstructUsing構造,然後將發生具體映射。我沒有看到我的值映射到具體的映射。映射有什麼問題嗎?這是一個有效的AutoMapper配置文件
檔案
CreateMap<TaskCustomProperty,TaskCustomPropertyDTO>()
.ConstructUsing (t => {
switch (t.Type) {
case "string":
return new TaskCustomPropertyString();
case "numeric":
return new TaskCustomPropertyNumeric();
case "choices":
return new TaskCustomPropertyChoices();
default:
throw new ArgumentOutOfRangeException();
}
})
.ForMember (property => property.Task, expression => expression.Ignore())
.Include<TaskCustomProperty, TaskCustomPropertyChoices>()
.Include<TaskCustomProperty, TaskCustomPropertyNumeric>()
.Include<TaskCustomProperty, TaskCustomPropertyString>();
CreateMap<TaskCustomProperty, TaskCustomPropertyChoices>()
.ForMember (property => property.Values, expression => expression.MapFrom (property => property.Choices != null ? string.Join ("|", property.Choices) : null))
.ForMember (property => property.StringValue, expression => expression.Ignore());
CreateMap<TaskCustomProperty, TaskCustomPropertyNumeric>()
.ForMember (property => property.Task, expression => expression.Ignore())
.ForMember (property => property.NumericValue, expression => expression.Ignore());
CreateMap<TaskCustomProperty, TaskCustomPropertyString>()
.ForMember (property => property.Task, expression => expression.Ignore())
.ForMember (property => property.StringValue, expression => expression.Ignore());
類
public class TaskCustomProperty {
public Guid? Id { get; set; }
public Guid TaskId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool IsRequired { get; set; }
public object Value { get; set; }
public string [] Choices { get; set; }
}
public abstract class TaskCustomPropertyDTO {
public Guid Id { get; set; }
public Guid TaskId { get; set; }
public string Name { get; set; }
public bool IsRequired { get; set; }
public abstract object Value { get; set; }
public abstract bool IsEmpty();
}
public class TaskCustomPropertyString : TaskCustomPropertyDTO {
public string StringValue { get; set; }
public override object Value {
get {
return StringValue;
}
set {
StringValue = (string)value;
}
}
public override bool IsEmpty() {
return string.IsNullOrWhiteSpace (StringValue);
}
}
public class TaskCustomPropertyChoices : TaskCustomPropertyString {
public string Values { get; set; }
public string [] GetValues() {
if (string.IsNullOrEmpty (Values)) {
return new string [0];
}
return Values.Split ('|');
}
}