为什么我不能在@Mapping属性中引用@Context参数?

时间:2020-09-05 10:28:31

标签: java mapstruct

使用mapstruct,我需要的是一种具有多个源的映射方法,并且将这几个源传递给其他映射方法,因此在需要这些其他源的情况下,我可以为所有映射方法使用所有多个源。

当前可以使两个功能协同工作:

  • 仅@Context可以传递给其他映射方法,但不能用作源。
  • 辅助参数(非@Context)可以用作源,但不会传递给其他映射方法

因此,该功能需要要么是允许将辅助源参数传递给其他映射方法,要么是使@Context参数能够被@Mapping(target="something", source="ctx.somethingElse")@Mapping(target="ctx.something", source="somethingElse)

示例:

// source classes : `Instant timestamp` is a field I obtain separately

Instant timestamp;

class WrapperSource
   List<NestedSource> nested;

class NestedSource
    String name;



// target classes : I want to map the nested and name field but also to insert the timestamp in both the WrapperTarget and every NestedTarget in the nested list

class WrapperTarget
   Instant timestamp;
   List<NestedTarget> nested;

class NestedTarget
    String name;
    Instant timestamp;

理想情况下,映射将类似于:

// Currently this doesn't work because we can't reference the @Context in the source attribute

@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, @Context Instant timestamp);

@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, @Context Instant timestamp);

或:

// Currently this doesn't work because the second method with 2 sources in not called by the first generated method

@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, Instant timestamp);

@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, Instant timestamp);

唯一适用于我的(详细)解决方法是:

// @Context is passed around and I can manually use it as a source in an @AfterMapping but it requires additional code

WrapperTarget map(WrapperSource source, @Context Instant timestamp);

@AfterMapping
void map(WrapperSource source, @MappingTarget WrapperTarget target, @Context Instant timestamp) {
    target.setTimestamp(timestamp);
}

NestedTarget map(NestedSource source, @Context Instant timestamp);

@AfterMapping
void map(NestedSource source, @MappingTarget NestedTarget target, @Context Instant timestamp) {
    target.setTimestamp(timestamp);
}

这可以正常工作,但是需要附加的手动代码,因此更好的替代方法是能够在@Context中引用@Mapping's attributes。这样,我可以使用第一个“理想”的映射示例。

此问题是否有更好的解决方法?

1 个答案:

答案 0 :(得分:2)

@Context应该是参数表明它是什么。映射的上下文信息,因此不参与映射本身。映射原则上是从源到目标。

请记住:MapStruct可以解决很多问题,但永远无法解决所有问题。

但是:您可以尝试以下方法:

class WrapperTarget implements TimeStamped
   Instant timestamp;
   List<NestedTarget> nested;

class NestedTarget implements TimeStamped
    String name;
    Instance timestamp;

interface TimeStamped{
   void setTimestamp(Instance timeStamp);
}

定义您自己的上下文... MapStruct在自动定义的上下文上调用after映射。您可以在这种情况下放置更多内容,例如在映射之前解析存储库中的内容,ID等...

class MyContext {

    Instance timestamp;

    @AfterMapping
    map(@MappingTarget TimeStamped timeStamped)
}

在这种情况下,您的映射与上下文无关。在调用该方法之前,需要先初始化上下文。也许您可以在上下文的构造中构造时刻(如果要求在所有位置都包含相同的时刻)。

@Mapping(target = "nested", source="source.nested")
WrapperTarget map(WrapperSource source, @Context MyContext ctx);

@Mapping(target = "name", source="source.name")
NestedTarget map(NestedSource source, @Context MyContext ctx);

要使用上下文,您可以查看this示例。