简体   繁体   English

使用自动映射器进行复杂映射

[英]complex mapping with automapper

I have a complex object (returned from a web service api).我有一个复杂的对象(从 Web 服务 api 返回)。 It has multiple nested items, some of which contain lists of property names & values (key value pairs).它有多个嵌套的项目,其中一些包含属性名称和值(键值对)的列表。 for example例如

a meterdetail class can contain multiple meter objects, each meter object has multiple rows, each row has a key (the property name) and a value (the propertyvalue).一个meterdetail 类可以包含多个meter 对象,每个meter 对象有多个行,每一行都有一个键(属性名称)和一个值(属性值)。 I need to map each row to a specific property of a class, so我需要将每一行映射到一个类的特定属性,所以

  1. row 1 key 'core' value maps to the 'Core' property of my meter class第 1 行键“核心”值映射到我的仪表类的“核心”属性
  2. row 2 key 'serial_number' value maps to the SerialNumber property of my meter class第 2 行键“serial_number”值映射到我的仪表类的 SerialNumber 属性
  3. row 3 key 'install_date' value maps to the InstallDate property of my meter class第 3 行键“install_date”值映射到我的仪表类的 InstallDate 属性

and so on等等

is this possible with automapper ?这可能与 automapper 吗? If so, how would I do this ?如果是这样,我将如何做到这一点? Im using .netcore 5 and C# Id greatly appreciate some advice我使用 .netcore 5 和 C# Id 非常感谢一些建议

I wouldn't bother.我不会打扰。 AutoMapper's great for simple 1-1 mappings from one type to another where they look basically the same, but if you did manage to do what you're asking with AutoMapper, you'd just be hiding complex logic in a mapping class away from where things are happening. AutoMapper 非常适合从一种类型到另一种类型的简单 1-1 映射,它们看起来基本相同,但是如果您确实设法使用 AutoMapper 完成了您的要求,那么您只是将复杂的逻辑隐藏在映射类中,远离事情正在发生。 If it were me, I'd spend the time literally just doing it by hand, instead.如果是我,我会把时间花在手工制作上,而不是。 You'll thank yourself for it later, I imagine.我想你以后会为此感谢自己的。

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

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