【问题标题】:Entity mapping library case for two differently structured entities两个不同结构实体的实体映射库案例
【发布时间】:2019-11-11 01:39:57
【问题描述】:

我一直在寻找一个实体映射库来避免我编写大量的属性映射代码。所以我找到了 AutoMapper、AgileMapper 和 Mapster。正如我所看到的,所有这些都有助于结构相似的实体。但就我而言,我的两个实体甚至完全不一样。

一个属性例如:

public class EntityA
{
    public int PropertyA;
}
public class EntityB
{
    public Inner1 inner1;
}
public class Inner1
{
    public Inner2 inner2;
}   
public class Inner2
{
    public double nothingLikeTheOtherPropName
}    

EntityA.PropertyA 映射到 Inner2.nothingLikeTheOtherPropName。

所以,问题是:如果两个实体在结构上不同,任何实体映射库都会有所帮助吗?

【问题讨论】:

  • 恐怕你得自己写映射代码。在发布的示例中,任何库如何猜测映射规则?所有映射器大多是基于约定的。它们可以配置为做一些自定义的事情,但在你的情况下,一切都是自定义的。因此,映射库不会有任何好处。

标签: c# mapping automapper


【解决方案1】:

AutoMapper 可以配置为映射不同的属性,但没有办法自动化完全不同的名称。 好处是你只做一次,在其他任何地方使用它都是正确的。

这是以前的答案

How to specify mapping rule when names of properties differ

【讨论】:

  • 是的,这听起来很合理。所以基本上这只是一场关于哪个库在性能方面做得更好的战斗。我的问题是手动操作比 AutoMapper 花费的时间少得多。
猜你喜欢
  • 2011-07-18
  • 2015-10-03
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
相关资源
最近更新 更多