Skip to content

Latest commit

 

History

History
63 lines (51 loc) · 1.82 KB

10-derived-type-mapping.md

File metadata and controls

63 lines (51 loc) · 1.82 KB

Derived types and interfaces

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Mapperly supports interfaces and base types as mapping sources and targets, but Mapperly needs to know which derived types exist. This can be configured with the MapDerivedTypeAttribute:

[Mapper]
public static partial class ModelMapper
{
    // highlight-start
    [MapDerivedType<Banana, BananaDto>] // for c# language level ≥ 11
    [MapDerivedType(typeof(Apple), typeof(AppleDto))] // for c# language level < 11
    // highlight-end
    public static partial FruitDto MapFruit(Fruit source);
}

abstract class Fruit {}
class Banana : Fruit {}
class Apple : Fruit {}

abstract class FruitDto {}
class BananaDto : FruitDto {}
class AppleDto : FruitDto {}
[Mapper]
public static partial class ModelMapper
{
    public static partial FruitDto MapFruit(Fruit source)
    {
        return source switch
        {
            Banana x => MapToBananaDto(x),
            Apple x => MapToAppleDto(x),
            _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to FruitDto as there is no known derived type mapping", nameof(source)),
        };
    }

    // ... implementations of MapToBananaDto and MapToAppleDto
}

All source types provided to the MapDerivedTypeAttribute need to implement or extend the type of the mapping method parameter. All target types provided to the MapDerivedTypeAttribute need to implement or extend the mapping method return type. Each source type has to be unique but multiple source types can be mapped to the same target type.