-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
docs/docs/01-getting-started/03-generated-mapper-example.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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](https://github.com/riok/mapperly/tree/main/samples/Riok.Mapperly.Sample). | ||
To view the generated code of your own mapper, refer to the [generated source configuration](../02-configuration/13-generated-source.mdx). | ||
|
||
## The source classes | ||
In this example, we have a car class with some general information. | ||
|
||
<CodeBlock language="csharp">{CarSource}</CodeBlock> | ||
|
||
## 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. | ||
|
||
<CodeBlock language="csharp">{CarDtoSource}</CodeBlock> | ||
|
||
## 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. | ||
|
||
<CodeBlock language="csharp">{CarMapperSource}</CodeBlock> | ||
|
||
## The generated code | ||
|
||
<CodeBlock language="csharp">{GeneratedCarMapperSource}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace Riok.Mapperly.Sample; | ||
|
||
public class Car | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
|
||
public int NumberOfSeats { get; set; } | ||
|
||
public CarColor Color { get; set; } | ||
|
||
public Manufacturer? Manufacturer { get; set; } | ||
|
||
public List<Tire> Tires { get; } = new List<Tire>(); | ||
} | ||
|
||
public enum CarColor | ||
{ | ||
Black = 1, | ||
Blue = 2, | ||
White = 3, | ||
} | ||
|
||
public class Manufacturer | ||
{ | ||
public Manufacturer(int id, string name) | ||
{ | ||
Id = id; | ||
Name = name; | ||
} | ||
|
||
public int Id { get; } | ||
|
||
public string Name { get; } | ||
} | ||
|
||
public class Tire | ||
{ | ||
public string Description { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace Riok.Mapperly.Sample; | ||
|
||
public class CarDto | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
|
||
public int NumberOfSeats { get; set; } | ||
|
||
public CarColorDto Color { get; set; } | ||
|
||
public ProducerDto? Producer { get; set; } | ||
|
||
public List<TireDto>? Tires { get; set; } | ||
} | ||
|
||
// Intentionally use different numeric values for demonstration purposes | ||
public enum CarColorDto | ||
{ | ||
Yellow = 1, | ||
Green = 2, | ||
Black = 3, | ||
Blue = 4, | ||
} | ||
|
||
// The manufacturer, but named differently for demonstration purposes | ||
public class ProducerDto | ||
{ | ||
public ProducerDto(int id, string name) | ||
{ | ||
Id = id; | ||
Name = name; | ||
} | ||
|
||
public int Id { get; } | ||
|
||
public string Name { get; } | ||
} | ||
|
||
public class TireDto | ||
{ | ||
public string Description { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Riok.Mapperly.Abstractions; | ||
|
||
namespace Riok.Mapperly.Sample; | ||
|
||
// Enums of source and target have different numeric values -> use ByName strategy to map them | ||
[Mapper(EnumMappingStrategy = EnumMappingStrategy.ByName)] | ||
public static partial class CarMapper | ||
{ | ||
[MapProperty(nameof(Car.Manufacturer), nameof(CarDto.Producer))] // Map property with a different name in the target type | ||
public static partial CarDto MapCarToDto(Car car); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json; | ||
using Riok.Mapperly.Sample; | ||
|
||
var car = new Car | ||
{ | ||
Name = "my car", | ||
NumberOfSeats = 5, | ||
Color = CarColor.Blue, | ||
Manufacturer = new Manufacturer(1, "best manufacturer"), | ||
Tires = | ||
{ | ||
new Tire { Description = "front left tire" }, | ||
new Tire { Description = "front right tire" }, | ||
new Tire { Description = "back left tire" }, | ||
new Tire { Description = "back right tire" }, | ||
}, | ||
}; | ||
|
||
var carDto = CarMapper.MapCarToDto(car); | ||
|
||
Console.WriteLine("Mapped car to car DTO:"); | ||
Console.WriteLine(JsonSerializer.Serialize(carDto, new JsonSerializerOptions { WriteIndented = true })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
|
||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
<ProjectReference Include="..\..\src\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
</ItemGroup> | ||
|
||
</Project> |