5

映射配置的伪代码(如下)是不可能的,因为 lambda 只允许我们访问 Type IDataReader,而在实际映射时,AutoMapper 将进入每个IDataRecordwhile的每个“单元格” IDataReader.Read() == true

var mappingConfig = Mapper.CreateMap<IDataReader, IEnumerable<MyDTO>>();
mappingConfig.ForMember(
    destination => destination.???,
    options => options.MapFrom(source => source.???));

谁能想到在运行时使用 AutoMapper 配置或仅满足以下要求的其他一些动态方法来做到这一点。

要求是支持任何IDataReader可能具有与属性名称不匹配的列名MyDTO并且没有我可以依赖的命名约定的传入。IDataReader相反,我们会在运行时要求用户将预期的列名与在via中找到的实际列名进行交叉引用IDataReader.GetSchemaTable()

4

1 回答 1

3

我不了解自动映射器,但我正在使用ValueInjecter将数据读取器映射到对象,如下所示:

 while (dr.Read())
 {
    var o = new User();
    o.InjectFrom<DataReaderInjection>(dr);
    return o;
 }

和 DataReaderInjection(类似于 Automapper 的 ValueResolver)

public class DataReaderInjection : KnownSourceValueInjection<IDataReader>
    {
        protected override void Inject(IDataReader source, object target, PropertyDescriptorCollection targetProps)
        {
            for (var i = 0; i < source.FieldCount; i++)
            {
                var activeTarget = targetProps.GetByName(source.GetName(i), true);
                if (activeTarget == null) continue;

                var value = source.GetValue(i);
                if (value == DBNull.Value) continue;

                activeTarget.SetValue(target, value);
            }
        }
    }

您可以使用它将值从 IDataReader 注入任何类型的对象


好的,所以根据你的要求,我想应该是这样的:

public class DataReaderInjection : KnownSourceValueInjection<IDataReader>
        {
            protected override void Inject(IDataReader source, object target, PropertyDescriptorCollection targetProps)
            {
                var columns = source.GetSchemaTable().Columns;
                for (var i = 0; i < columns.Count; i++)
                {
                    var c = columns[i];

                    var targetPropName = c.ColumnName; //default is the same as columnName

                    if (c.ColumnName == "Foo") targetPropName = "TheTargetPropForFoo";
                    if (c.ColumnName == "Bar") targetPropName = "TheTargetPropForBar";
                    //you could also create a dictionary and use it here

                    var targetProp = targetProps.GetByName(targetPropName);
                    //go to next column if there is no such property in the target object
                    if (targetProp == null) continue;

                    targetProp.SetValue(target, columns[c.ColumnName]);
                }
            }
        }

在这里我使用了GetSchemaTable,就像你想要的那样:)


好的,如果您想将一些东西传递给注入,您可以通过多种方式进行,方法如下:

o.InjectFrom(new DataReaderInjection(stuff), dr);
//you need a constructor with parameters for the DataReaderInjection in this case

var ri = new DataReaderInjection();
ri.Stuff = stuff;
o.InjectFrom(ri, dr);
//you need to add a property in this case

这是一个提示(对于带参数的构造函数方式)

  public class DataReaderInjection : KnownSourceValueInjection<IDataReader>
     {
        private IDictionary<string, string> stuff;
        public DataReaderInjection(IDictionary<string,string> stuff)
        {
          this.stuff = stuff;
        }
                    protected override void Inject(
...
于 2010-06-17T12:07:04.607 回答