简体   繁体   中英

How to map a composition relationship?

I'd like to map a composition without using the deep mapping property of 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

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.

I'd like to respect DRY (Don't Repeat Yourself).

You do not have to specify deep mappings, you should be able to say how B1 is mapped to B2 as you said:

<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):

<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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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