简体   繁体   English

Automapper是否可以将字段映射到具体类

[英]Is it possible for Automapper to map fields to concrete classes

I have a interface, with two classes that inherit from it. 我有一个接口,有两个从其继承的类。

public interface IField
{
    int FieldID { get; set; }
    string FieldName { get; set; }
    string FieldType { get; }
    string FieldDisplayValue { get; set; }
}

public abstract class BaseField : IField
{
    public int FieldID { get; set; }
    public string FieldName { get; set; }
    public abstract string FieldType { get; }
    public string FieldDisplayValue { get; set; }
}

public class StaticText : BaseField, IField
{
    public override string FieldType => "Text";
}

public class TextField : BaseField, IField
{
    public override string FieldType => "TextBox";
    public bool Required { get; set; }
    public int MinimumLength { get; set; }
    public int MaximumLength { get; set; }
    public string DefaultText { get; set; }
}

I would like to automap a generic class to one of these classes, based on a type included in the incoming class. 我想根据传入类中包含的类型将通用类自动映射到这些类之一。

public class FieldViewModel
{
    public int FieldID { get; set; }
    public string FieldName { get; set; }
    public  string FieldType { get; set;  }
    public string FieldDisplayValue { get; set; }
    public bool Required { get; set; }
    public int MinimumLength { get; set; }
    public int MaximumLength { get; set; }
    public string DefaultText { get; set; }

}

I have constructed a factory class that will return the appropriate object based on the fieldType. 我已经构造了一个工厂类,它将根据fieldType返回适当的对象。 I have successfully gotten automapper to use the factory to create the correct concrete class, but it will only map fields from the interface, not any of the extra fields defined in the concrete classes. 我已经成功地获得了自动映射器,可以使用工厂来创建正确的具体类,但是它将仅映射接口中的字段,而不会映射具体类中定义的任何其他字段。

my map for the interface to concrete classes looks like this: 我的具体类接口图如下:

cfg.CreateMap<FieldViewModel, IField>()
                .ConstructUsing(x => FieldFactory.CreateField(x.FieldType));

var iMapper = config.CreateMapper();

var field = iMapper.Map<FieldViewModel, IField>(sourceObject);

when passing in a source class that contains required, minimum, or maximum, those fields are not being mapped in the newly created concrete class. 当传入包含必需,最小值或最大值的源类时,这些字段不会映射到新创建的具体类中。

Is this even possible to do with automapper? automapper甚至可以做到吗? or do I need to map these fields myself? 还是我需要自己映射这些字段?

after a bit more research, it looks like what I was missing was the beforeMap code. 经过更多研究后,看来我缺少的是beforeMap代码。

cfg.CreateMap<FieldViewModel, IField>()
                .ConstructUsing(x => FieldFactory.CreateField(x.FieldType))
                .BeforeMap((s,d,c) => c.Mapper.Map(s, d));
            cfg.CreateMap<FieldViewModel, StaticText>();
            cfg.CreateMap<FieldViewModel, TextField>();

ended up having to create map for each derived concrete class, but the before map handles making sure the right one gets mapped with data. 最终不得不为每个派生的具体类创建地图,但是before地图会处理确保正确的人被数据映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM