这是一个有效的AutoMapper配置文件

时间:2013-08-27 17:53:22

标签: automapper

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

配置文件

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

1 个答案:

答案 0 :(得分:0)

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

请参阅:https://github.com/AutoMapper/AutoMapper/issues/376