Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.75 KB

03-generated-mapper-example.mdx

File metadata and controls

37 lines (24 loc) · 1.75 KB

import CodeBlock from '@theme/CodeBlock'; import CarSource from '!!raw-loader!../../src/data/generated/samples/Car.cs'; import CarDtoSource from '!!raw-loader!../../src/data/generated/samples/CarDto.cs'; import CarMapperSource from '!!raw-loader!../../src/data/generated/samples/CarMapper.cs'; import GeneratedCarMapperSource from '!!raw-loader!../../src/data/generated/samples/CarMapper.g.cs';

Generated mapper example

This example will show you what kind of code Mapperly generates. It is based on the Mapperly sample. To view the generated code of your own mapper, refer to the generated source configuration.

The source classes

In this example, we have a car class with some general information.

{CarSource}

The target classes

Our sample target classes are mostly the same as the source classes. For demonstration purposes, we named the manufacturer property differently to show how Mapperly can handle this use case.

{CarDtoSource}

The mapper

The actual mapper is pretty simple. We use a static mapper class in this example. As usual, we need to mark the mapper with the [Mapper] attribute, so that the source generator is able to recognize it. Because the manufacturer/producer properties are named differently, we also need to configure that via an attribute. In addition, because the CarColor and CarColorDto entries have different numeric values, we need to configure Mapperly to map them by their name.

{CarMapperSource}

The generated code

{GeneratedCarMapperSource}