简体   繁体   English

您可以使用automapper对所有非映射属性执行特定操作吗?

[英]Can you do a specific action with all non mapped properties using automapper?

I'm implementing an import feature that will read a spreadsheet file and save all rows as records on a DB . 我正在实现一个导入功能,它将读取电子表格文件并将所有行保存为DB记录。 I already have code in place that transforms the spreadsheet into a DataSet . 我已经有代码将电子表格转换为DataSet

The problem I have right now is that the class I need to deserialize the DataRows to is something like this: 我现在DataRows的问题是我需要将DataRows反序列化的类是这样的:

public class Product
{
    public string Name { get; set; }

    // More Props

    public IDictionary<string, object> Specifications { get; set; }
}

And each DataRow will have all the Product properties and some extra that have to be added into the Specifications Dictionary . 每个DataRow都将具有所有Product属性和一些额外的必须添加到Specifications Dictionary

Is there a way to configure AutoMapper to map all non existent properties in the destination into a new item on the Specifications Dictionary ? 有没有办法配置AutoMapper将目标中所有不存在的属性映射到Specifications Dictionary的新项目?

You can't do a specific action but you can create a custom mapping which finds all unmapped properties and adds them to the destination dictionary. 您无法执行特定操作,但可以创建自定义映射,查找所有未映射的属性并将其添加到目标字典。

This is a little trickier because you are mapping from a DataRow , rather than a normal class, but it can be done. 这有点棘手,因为您是从DataRow而不是普通类映射,但它可以完成。 In your resolution method for Specifications , you need to loop through the columns of the DataTable and check if there is a mapping for a property with that name. Specifications的解决方法中,您需要遍历DataTable的列,并检查是否存在具有该名称的属性的映射。 If not add it to the dictionary: 如果没有将它添加到字典中:

    private static void ConfigureMappings()
    {
        Mapper.CreateMap<DataRow, Product>()
            .ForMember(p => p.Name, mo => mo.MapFrom(row => row["Name"]))
            .ForMember(p => p.ID, mo => mo.MapFrom(row => row["ID"]))
            .ForMember(p => p.Specifications, mo => mo.ResolveUsing(MapSpecifications));
    }

    private static object MapSpecifications(ResolutionResult rr, DataRow row)
    {
        Dictionary<string, object> specs = new Dictionary<string, object>();

        // get all properties mapped in this mapping
        var maps = rr.Context.Parent.TypeMap.GetPropertyMaps();

        // loop all columns in the table
        foreach (DataColumn col in row.Table.Columns)
        {
            // if no property mapping exists, get value and add to dictionary
            if (!maps.Any(map => map.DestinationProperty.Name == col.ColumnName))
            {
                specs.Add(col.ColumnName, row[col.ColumnName]);
            }
        }

        return specs;
    }

Sample dotnetFiddle 示例dotnetFiddle

暂无
暂无

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

相关问题 自动映射器查找未映射的属性 - Automapper Finding Not Mapped properties 当我使用 AutoMapper 时,如果这些属性具有相同的名称,我是否需要明确地编写所有属性? - Do I need to write all the properties explicitly when I am using AutoMapper, if these properties have the same name? 如何让 AutoMapper:忽略非映射属性,包括常规映射并使用 QueryableExtensions - How get AutoMapper to: ignore non mapped properties, include conventional mapping and work with QueryableExtensions 使用 AutoMapper 如何在映射之前验证源值? - Using AutoMapper how can a source value be validated before being mapped? 为什么在使用记录时必须忽略 AutoMapper 中的所有其他属性? - Why do I have to Ignore all other properties in AutoMapper when using records? 你能告诉 AutoMapper 在映射时全局忽略缺失的属性吗? - Can you tell AutoMapper to globally ignore missing properties when mapping? 你能否让AutoMapper只显示明确匹配的地图属性 - Can you have AutoMapper only map properties that match explicitly 如何在使用Automapper时忽略特定类型的属性? - How to ignore properties of a specific type when using Automapper? 如何序列化NHibernate映射对象的所有属性? - How do I serialize all properties of an NHibernate-mapped object? 自动映射器可以处理某些属性不匹配的情况吗? - Can automapper handle a case where the some of the properties do not match?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM