0

我希望 Destination 将使用 ConstructUsing 构造,然后会发生具体映射。我没有看到我的值映射到具体映射上。映射有问题吗?

轮廓

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 ('|');
    }
}
4

1 回答 1

0

ConstructUsing 不像对象的工厂那样工作。

见:https ://github.com/AutoMapper/AutoMapper/issues/376

于 2013-09-05T02:31:07.730 回答