简体   繁体   English

如何映射构图关系?

[英]How to map a composition relationship?

I'd like to map a composition without using the deep mapping property of Dozer. 我想在不使用Dozer的深度映射属性的情况下映射合成。

Another important element is that the composed class and the main class map to the same class 另一个重要元素是组成类和主类映射到同一类

I've searched on SO, the Dozer documentation (which is good by the way), but I still, haven't found what I'm looking for 我已经搜索过推土机文档(这很好),但是我仍然没有找到我想要的东西

Example : A and B -> C 示例: A和B-> C

Class A {
  B b
  a1
}

Class B {
  b1
  b2
}

Class C {
  ca1
  cb1
  cb2

}

I don't want to map like : 我不想像这样映射:

<mapping>
  <class-a>A</class-a>
  <class-b>C</class-b>
  <field>
    <a>b.b1</a>
    <b>cb1</b>
  </field>
  etc.
</mapping>

I'd like to use something like : 我想使用类似:

<mapping>
  <class-a>B</class-a>
  <class-b>C</class-b>
  <field>
    <a>b1</a>
    <b>cb1</b>
  </field>
  etc.
</mapping>
<mapping>
  <class-a>A</class-a>
  <class-b>C</class-b>
  <field>
    <a>a1</a>
    <b>ca1</b>
  </field>
</mapping>

My class B is used with composition in others classes. 我的班级B与其他班级的作文一起使用。

I'd like to respect DRY (Don't Repeat Yourself). 我想尊重DRY(不要重复自己)。

You do not have to specify deep mappings, you should be able to say how B1 is mapped to B2 as you said: 您不必指定深层映射,您应该能够说出B1如何映射到B2:

<mapping>
  <class-a>B1</class-a>
  <class-b>B2</class-b>
  <field>
    <a>b1Param1</a>
    <b>b2Param1</b>
  </field>
</mapping>

And as a separate mapping how A1 is mapped to A2 (but not the sub mappings of the composed classes): 作为单独的映射,如何将A1映射到A2(但不映射组成类的子映射):

<mapping>
  <class-a>A1</class-a>
  <class-b>A2</class-b>
  <field>
    <a>b1</a>
    <b>b2</b>
  </field>
</mapping>

Dozer will recognise that b1 and b2 are objects of specific classes and look for a mapping between the two, which can be provided as a separate mapping. 推土机将认识到b1和b2是特定类的对象,并寻找两者之间的映射,可以将其作为单独的映射提供。

There is no solution, except using deep mapping and repeating it, unfortunately. 不幸的是,除了使用深度映射并重复它之外,没有其他解决方案。
The problem would be for Dozer to map C -> A and B. It's not easily possible. 问题是Dozer映射C-> A和B。这不容易实现。

One solution would be to able this type of auto-mapping in the case of "one-way" mapping. 一种解决方案是在“单向”映射的情况下能够进行这种类型的自动映射。

Complex problem, similar to this Dozer FAQ 复杂问题,类似于此推土机常见问题解答

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

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