”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 使用 MapStruct 映射继承层次结构

使用 MapStruct 映射继承层次结构

发布于2024-11-08
浏览:233

Mapping inheritance hierarchies with MapStruct

Intro

MapStruct provides a rich set of features for mapping Java types. The technical documentation describes extensively the classes and annotations provided by MapStruct and how to use them. More complex use cases are described in several community written articles all over the web. To complement the pool of available articles, this article will focus on mapping inheritance hierarchies and provide a possible solution that offers simplicity and reusability. I assume that the reader has a basic knowledge of MapStruct. If you're interested in a running example, feel free to check out this repo and try things out.

Example

To demonstrate the capabilities of MapStruct in a simple way, we will use a very small and thus useless domain model for which the use of MapStruct seems overly complex, but which allows the code snippets to remain simple throughout the article. The real benefits of MapStruct become apparent especially with larger models.

// Source classes
public class SourceProject {
  private String name;
  private LocalDate dueDate;
  // getters   setters omitted throughout the code
}

// Target classes
public class TargetProject {
  private ProjectInformation projectInformation;
}

public class ProjectInformation {
  private String projectName;
  private LocalDate endDate;
}

As you can see, the source and target entities express the same information but are structured slightly differently. A mapper can be defined like this...

@Mapper
public interface ProjectMapper {
  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  TargetProject mapProject(SourceProject source);
}

...and MapStruct will generate code that will look like this:

public class ProjectMapperImpl implements ProjectMapper {

    @Override
    public TargetProject mapProject(SourceProject source) {
        if ( source == null ) {
            return null;
        }

        TargetProject targetProject = new TargetProject();

        targetProject.setProjectInformation( sourceProjectToProjectInformation( source ) );

        return targetProject;
    }

    protected ProjectInformation sourceProjectToProjectInformation(SourceProject sourceProject) {
        if ( sourceProject == null ) {
            return null;
        }

        ProjectInformation projectInformation = new ProjectInformation();

        projectInformation.setProjectName( sourceProject.getName() );
        projectInformation.setEndDate( sourceProject.getDueDate() );

        return projectInformation;
    }
}

Now let's introduce some new entities that use inheritance:

// Source classes
@Data
public class SourceScrumProject extends SourceProject {
  private Integer velocity;
}

// Target classes
@Data
public class TargetScrumProject extends TargetProject {
  private Velocity velocity;
}

@Data
public class Velocity {
  private Integer value;
}

If we want to use the parent mapper universally to map both the parent entity and child entities, we can use the @SubclassMapping annotation, which generates a dispatching via instanceof checks to the mapping of possible child classes.

@Mapper
public interface ProjectMapper {
  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  @SubclassMapping(source = SourceScrumProject.class, target = TargetScrumProject.class)
  TargetProject mapProject(SourceProject source);

  @Mapping(target = "velocity.value", source = "velocity")
  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  TargetScrumProject mapScrumProject(SourceScrumProject source);
}

This generates the following code.

public class ProjectMapperImpl implements ProjectMapper {

    @Override
    public TargetProject mapProject(SourceProject source) {
        if ( source == null ) {
            return null;
        }

        if (source instanceof SourceScrumProject) {
            return mapScrumProject( (SourceScrumProject) source );
        }
        else {
            TargetProject targetProject = new TargetProject();

            targetProject.setProjectInformation( sourceProjectToProjectInformation( source ) );

            return targetProject;
        }
    }

    @Override
    public TargetScrumProject mapScrumProject(SourceScrumProject source) {
        if ( source == null ) {
            return null;
        }

        TargetScrumProject targetScrumProject = new TargetScrumProject();

        targetScrumProject.setVelocity( sourceScrumProjectToVelocity( source ) );
        targetScrumProject.setProjectInformation( sourceScrumProjectToProjectInformation( source ) );

        return targetScrumProject;
    }

    protected ProjectInformation sourceProjectToProjectInformation(SourceProject sourceProject) {
        if ( sourceProject == null ) {
            return null;
        }

        ProjectInformation projectInformation = new ProjectInformation();

        projectInformation.setProjectName( sourceProject.getName() );
        projectInformation.setEndDate( sourceProject.getDueDate() );

        return projectInformation;
    }

    protected Velocity sourceScrumProjectToVelocity(SourceScrumProject sourceScrumProject) {
        if ( sourceScrumProject == null ) {
            return null;
        }

        Velocity velocity = new Velocity();

        velocity.setValue( sourceScrumProject.getVelocity() );

        return velocity;
    }

    protected ProjectInformation sourceScrumProjectToProjectInformation(SourceScrumProject sourceScrumProject) {
        if ( sourceScrumProject == null ) {
            return null;
        }

        ProjectInformation projectInformation = new ProjectInformation();

        projectInformation.setProjectName( sourceScrumProject.getName() );
        projectInformation.setEndDate( sourceScrumProject.getDueDate() );

        return projectInformation;
    }
}

We can already see some problems here:

  1. We are duplicating the @Mapping annotation from the parent mapping.
  2. Parts of the generated code are duplicated (sourceProjectToProjectInformation and sourceScrumProjectToProjectInformation).
  3. The interface becomes wider as it contains mapping methods for both the parent and child entities.

With only these two fields this doesn't seem terrible, but imagine what the generated code would look like if we had more child classes containing more fields. The effect would be much bigger.

Let's try to tackle problem #1. MapStruct offers the annotation @InheritConfiguration which allows us to reuse mapping configuration from either the same class or the mapping configuration class used:

@Mapper
public interface ProjectMapper {
  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  @SubclassMapping(source = SourceScrumProject.class, target = TargetScrumProject.class)
  TargetProject mapProject(SourceProject source);

  @Mapping(target = "velocity.value", source = "velocity")
  @InheritConfiguration(name = "mapProject")
  TargetScrumProject mapScrumProject(SourceScrumProject source);
}

This at least saves us a lot of duplicate configuration. Spoiler: We will not want to use this anymore at a later stage. But let's first tackle problem #2 and #3.

Since we could have potentially wide interfaces with a lot of duplicated code, using, understanding and debugging of the generated code could become more difficult. It would be easier if we had a mapper for each subclass that is self-contained and only either dispatches to a child mapper or performs a mapping, but not both. So let's move the mapping of the Scrum projects to a separate interface.

@Mapper(uses = ScrumProjectMapper.class)
public interface ProjectMapper {
  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  @SubclassMapping(source = SourceScrumProject.class, target = TargetScrumProject.class)
  TargetProject mapProject(SourceProject source);
}

@Mapper
public interface ScrumProjectMapper {
  @Mapping(target = "velocity.value", source = "velocity")
  @InheritConfiguration(name = "mapProject") // not working
  TargetScrumProject mapScrumProject(SourceScrumProject source);
}

We tell ProjectMapper to dispatch the mapping of ScrumProjects to ScrumProjectMapper via the uses-clause. The problem here is that the configuration from the mapProject-method is no longer visible to the ScrumProjectMapper. We could of course let it extend ProjectMapper, but then we have the problem of the wide interface and duplicated code again, as all methods are merged into ScrumProjectMapper. We could instead make ProjectMapper a config using the @MapperConfig annotation and reference it in ScrumProjectMapper, but since it also uses ScrumProjectMapper in the uses-clause to enable the dispatching, MapStruct would complain about the circular dependency. Furthermore, if we have an inheritance hierarchy with a height > 1, we quickly notice that MapStruct does not pass the config down the mapper hierarchy more than one level, making the config at level 0 unavailable on levels 2 and beyond.

Fortunately, there is a solution. The @Mapping annotation can be applied to other annotations. By declaring an annotation ProjectMappings, which basically wraps all the mapping information for Projects, we can reuse it anywhere we want. Let's see what this could look like.

@Mapper(uses = ScrumProjectMapper.class)
public interface ProjectMapper {
  @Mappings
  @SubclassMapping(source = SourceScrumProject.class, target = TargetScrumProject.class)
  TargetProject mapProject(SourceProject source);

  @Mapping(target = "projectInformation.projectName", source = "name")
  @Mapping(target = "projectInformation.endDate", source = "dueDate")
  @interface Mappings {
  }
}

@Mapper
public interface ScrumProjectMapper {
  @Mapping(target = "velocity.value", source = "velocity")
  @ProjectMapper.Mappings
  TargetScrumProject mapScrumProject(SourceScrumProject source);
}

Imagine that we have more child classes than just ScrumProject. By simply bundling the mapping information in the shared annotation we can centralize the information and avoid all the pitfalls that come with duplication. This also works for deeper inheritance hierarchies. I just need to annotate my mapping method with the @Mappings-annotation of the parent mapper that uses the annotation of its parent mapper, and so on.

We can see in the generated code now that the mappers either dispatch or do the mapping only for the classes they're built for:

public class ProjectMapperImpl implements ProjectMapper {

    private final ScrumProjectMapper scrumProjectMapper = Mappers.getMapper( ScrumProjectMapper.class );

    @Override
    public TargetProject mapProject(SourceProject source) {
        if ( source == null ) {
            return null;
        }

        if (source instanceof SourceScrumProject) {
            return scrumProjectMapper.mapScrumProject( (SourceScrumProject) source );
        }
        else {
            TargetProject targetProject = new TargetProject();

            targetProject.setProjectInformation( sourceProjectToProjectInformation( source ) );

            return targetProject;
        }
    }

    protected ProjectInformation sourceProjectToProjectInformation(SourceProject sourceProject) {
        if ( sourceProject == null ) {
            return null;
        }

        ProjectInformation projectInformation = new ProjectInformation();

        projectInformation.setProjectName( sourceProject.getName() );
        projectInformation.setEndDate( sourceProject.getDueDate() );

        return projectInformation;
    }
}

public class ScrumProjectMapperImpl implements ScrumProjectMapper {

    @Override
    public TargetScrumProject mapScrumProject(SourceScrumProject source) {
        if ( source == null ) {
            return null;
        }

        TargetScrumProject targetScrumProject = new TargetScrumProject();

        targetScrumProject.setVelocity( sourceScrumProjectToVelocity( source ) );
        targetScrumProject.setProjectInformation( sourceScrumProjectToProjectInformation( source ) );

        return targetScrumProject;
    }

    protected Velocity sourceScrumProjectToVelocity(SourceScrumProject sourceScrumProject) {
        if ( sourceScrumProject == null ) {
            return null;
        }

        Velocity velocity = new Velocity();

        velocity.setValue( sourceScrumProject.getVelocity() );

        return velocity;
    }

    protected ProjectInformation sourceScrumProjectToProjectInformation(SourceScrumProject sourceScrumProject) {
        if ( sourceScrumProject == null ) {
            return null;
        }

        ProjectInformation projectInformation = new ProjectInformation();

        projectInformation.setProjectName( sourceScrumProject.getName() );
        projectInformation.setEndDate( sourceScrumProject.getDueDate() );

        return projectInformation;
    }
}

I'd say this makes understanding and debugging a single mapper easier. As we've already covered a lot of ground, let's wrap things up at this point. There are still some edge cases that can lead to further problems, but these will be covered in the next part.

Wrap-up

Writing mappers with MapStruct for inheritance hierarchies should be a common task and easy to achieve, but you can quickly get stuck in some of MapStruct's quirks. Mapping the entire hierarchy in one class results in large classes implementing wide interfaces that are difficult to read and debug. When splitting the mappers into one per class, we want to reuse the mapping information from the parent mappers to avoid duplicating mapping information. Extending the parent mapper to make its mapping configuration visible for use in @InheritConfiguration is not desirable, as we will again have the problem of a wide interface with a lot of duplicated code. Using the parent mapper as a config is also not possible due to circular dependencies. We could see that the creating a custom annotation that bundles the mapping information for use in child mappers solves the problem. By additionally using SubclassMapping, the parent mapper provides the bundled information on how to map the entity it knows, contains only the mapping for exactly that class and dispatches the mapping of any other child entities down the hierarchy of mappers.

版本声明 本文转载于:https://dev.to/jholsch/mapping-inheritance-hierarchies-with-mapstruct-7p0?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • ## 如何在 JavaScript 中限制函数执行:自定义解决方案与库解决方案
    ## 如何在 JavaScript 中限制函数执行:自定义解决方案与库解决方案
    通过自定义实现实现 JavaScript 中的简单节流使用 JavaScript 时,控制函数执行速率至关重要。节流函数限制函数调用的频率,防止繁重的处理或重复的用户操作。在这篇文章中,我们提出了一个简单的自定义节流函数来实现此目的,而不依赖于 Lodash 或 Underscore 等外部库。 提...
    编程 发布于2024-11-08
  • 了解 WebSocket:React 开发人员综合指南
    了解 WebSocket:React 开发人员综合指南
    Understanding WebSockets: A Comprehensive Guide for React Developers In today’s world of modern web applications, real-time communication is ...
    编程 发布于2024-11-08
  • 如何在 macOS 上安装并启用 Imagick for PHP
    如何在 macOS 上安装并启用 Imagick for PHP
    如果您在 macOS 上工作并且需要安装 Imagick for PHP 8.3,则可能会遇到默认安装较旧版本 PHP(例如 PHP 8.0)的问题。在这篇文章中,我将引导您完成确保 Imagick 已安装并针对 PHP 8.3 正确配置的步骤。 第 1 步:通过 Homebrew ...
    编程 发布于2024-11-08
  • 如何使用 JavaScript 为对象数组添加附加属性?
    如何使用 JavaScript 为对象数组添加附加属性?
    扩展具有附加属性的对象数组编程中普遍存在的任务涉及使用附加属性增强现有对象数组。为了说明这个概念,请考虑包含两个元素的对象数组:Object {Results:Array[2]} Results:Array[2] [0-1] 0:Object id=1 name: "R...
    编程 发布于2024-11-08
  • 如何解决 CSS 中可变字体的文本笔划问题?
    如何解决 CSS 中可变字体的文本笔划问题?
    文本描边难题:解决 CSS 兼容性问题使用 -webkit-text-lines 创建引人注目的文本效果是网页设计师的一项基本技术。但是,当将此属性与可变字体一起使用时,可能会出现意外的笔划行为。这种不一致不仅限于 Chrome,而是不同浏览器中更普遍的问题。问题的症结:可变字体和笔画冲突可变字体具...
    编程 发布于2024-11-08
  • C++ 中的私有虚拟方法:平衡封装和重写
    C++ 中的私有虚拟方法:平衡封装和重写
    了解 C 中私有虚拟方法的好处 在面向对象编程中,私有方法封装实现细节并限制其在一个班级。然而,在 C 中,虚函数提供后期绑定并允许对象的多态行为。通过结合这些概念,私有虚拟方法提供了独特的优势。考虑以下用法,其中 HTMLDocument 继承自多个基类:class HTMLDocument : ...
    编程 发布于2024-11-08
  • 斋浦尔数据科学研究所:传统与技术的邂逅
    斋浦尔数据科学研究所:传统与技术的邂逅
    斋浦尔,粉红之城,长期以来一直是一座拥有丰富文化遗产、雄伟宫殿和充满活力的传统的城市,但这座城市的另一个特征是教育和技术进步。这是通过斋浦尔的几个数据科学研究所推出的,通过这些机构引导学生和专业人士进入快速变化的技术世界。 这些机构融合了传统与创新,在培养这座城市的未来科技人才方面发挥着重要作用。在...
    编程 发布于2024-11-08
  • 如何根据多个条件过滤 JavaScript 对象数组?
    如何根据多个条件过滤 JavaScript 对象数组?
    基于多个条件过滤JavaScript中的数组问题陈述给定一个对象数组和一个过滤器对象,目标是过滤和根据过滤器中指定的多个条件简化数组。但是,当过滤器包含多个属性时,会出现一个特定问题。建议的解决方案考虑以下代码段:function filterUsers(users, filter) { var...
    编程 发布于2024-11-08
  • 理解 Laravel 11 中 pluck() 和 select() 之间的区别
    理解 Laravel 11 中 pluck() 和 select() 之间的区别
    Laravel 是最流行的 PHP 框架之一,提供了一系列强大的数据操作方法。其中,pluck() 和 select() 在处理集合时经常使用。尽管它们看起来相似,但它们的目的不同。在本文中,我们将探讨这两种方法之间的差异,解释何时使用每种方法,并提供实际的编码示例来演示它们在 Laravel 11...
    编程 发布于2024-11-08
  • 什么是 Cloudflare? Web 性能和安全公司概述
    什么是 Cloudflare? Web 性能和安全公司概述
    在快节奏的数字世界中,网站的速度、安全性和可靠性对于企业和用户都至关重要。 Cloudflare 已成为确保网站平稳、安全和高效运行的基石。但 Cloudflare 到底是什么?为什么它成为网站所有者如此重要的工具?让我们深入了解它的作用和产品。 Cloudflare 简介 Clou...
    编程 发布于2024-11-08
  • 如何优化 MySQL 索引性能以加快查询速度?
    如何优化 MySQL 索引性能以加快查询速度?
    优化MySQL索引性能要有效检查MySQL索引的性能,可以使用以下查询:EXPLAIN EXTENDED SELECT col1, col2, col3, COUNT(1) FROM table_name WHERE col1 = val GROUP BY col1 ORDER BY col...
    编程 发布于2024-11-08
  • 如何在 PHP 中将数据添加到文件中?
    如何在 PHP 中将数据添加到文件中?
    PHP 中的文件追加与前置在 PHP 中使用“a”(append ) 模式。然而,写入文件的开头需要更细致的方法。在所描述的场景中,“r”模式(读写)允许添加数据,但会覆盖以前的内容。为了避免这种限制,需要更复杂的技术。使用 file_put_contents() 的解决方案该解决方案涉及将 fil...
    编程 发布于2024-11-08
  • 为什么在 C++ 中打印函数名称会导致“1”?
    为什么在 C++ 中打印函数名称会导致“1”?
    在不调用的情况下计算函数:解开谜团想象一下:您正在编码,而不是调用带括号的函数,您只需打印它的名称即可。令人惊讶的是,结果总是 1。这种非常规的方法让您感到困惑,无论是关于 1 还是缺少预期的函数指针。让我们深入研究代码的复杂性:#include <iostream> using nam...
    编程 发布于2024-11-08
  • 软件开发中的左移测试:完整指南
    软件开发中的左移测试:完整指南
    左移测试是一种旨在通过将测试流程移至开发生命周期的早期,在问题升级之前解决问题来提高软件质量的策略。传统上,测试是在开发周期即将结束时进行的,但这通常会由于较晚发现缺陷而导致更高的成本和更长的时间。通过“左移”,团队旨在及早预防问题,培养主动而非被动的质量保证方法。 随着敏捷和 DevOps 方法...
    编程 发布于2024-11-08
  • Infusion 文档生成 CLI 工具
    Infusion 文档生成 CLI 工具
    Infusion 是一个开源工具,用于在代码文件中生成文档。它使用OpenAI gpt-4模型来编写注释。这是我的项目,我用 Python 编写的。 GitHub 链接: https://github.com/SychAndrii/infusion explainer.js 是一个开源工具,用于解释...
    编程 发布于2024-11-08

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3