Skip to content

Douaa1819/Mapstruct-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 MapStruct Practical Guide

A concise guide to MapStruct, a Java framework for efficient object mapping. This guide compares MapStruct with ModelMapper, shows SOLID principles in practice, and highlights key annotations.

📋 Table of Contents

  1. Introduction
  2. MapStruct vs ModelMapper
  3. SOLID Principles
  4. Key Annotations
  5. Resources

💡 Introduction

MapStruct generates type-safe mapping code at compile-time, avoiding reflection, and making object mapping fast, reliable, and easy to maintain.


🔄 MapStruct vs ModelMapper

Feature MapStruct ModelMapper
How it works Compile-time code generation Runtime reflection to map fields
Performance High, optimized pre-generated code Lower, reflection adds runtime overhead
Ease of maintenance Clear annotations, easy to debug Flexible but less intuitive, errors at runtime

⚙️ SOLID Principles

  • Single Responsibility Principle (SRP): Each mapper class has a single responsibility — mapping objects.
  • Open/Closed Principle (OCP): Mappers can be extended/configured via annotations without changing existing code.
  • Dependency Inversion Principle (DIP): @Mapper enables dependency injection, easing testing and extension.

🔖 Key Annotations

Annotation Description
@Mapper Declares a MapStruct mapper interface
@Mapping Defines mapping rules between source and target fields
@MappingTarget Updates an existing object instead of creating a new one
@AfterMapping Defines a post-processing method to run after mapping
@BeforeMapping Defines a method to execute before mapping
@ValueMapping Maps values between enums

📚 Resources

About

A practical guide to learning MapStruct, a fast and efficient Java object mapping library. This repository includes examples of basic and nested mappings, a comparison with ModelMapper, and explanations of the SOLID principles applied.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages