Skip to main content

Mapper configuration

The MapperAttribute provides options to customize the generated mapper class.

Copy behaviour

By default, Mapperly does not create deep copies of objects to improve performance. If an object can be directly assigned to the target, it will do so (eg. if the source and target type are both Car[], the array and its entries will not be cloned). To create deep copies, set the UseDeepCloning property on the MapperAttribute to true.

[Mapper(UseDeepCloning = true)]
public partial class CarMapper
{
...
}

Properties

On each mapping method declaration, property mappings can be customized. If a property on the target has a different name than on the source, the MapPropertyAttribute can be applied. If a property should be ignored, the MapperIgnoreTargetAttribute or MapperIgnoreSourceAttribute can be used.

[Mapper]
public partial class CarMapper
{
[MapProperty(nameof(Car.Model), nameof(CarDto.ModelName))]
[MapperIgnoreTarget(nameof(CarDto.MakeId))]
[MapperIgnoreSource(nameof(Car.Id))]
public partial CarDto ToDto(Car car);
}

Property name mapping strategy

By default, property names are matched using a case sensitive strategy. If all properties differ only in casing, for example ModelName on the source and modelName on the target, the [MapperAttribute] can be used with the PropertyNameMappingStrategy option.

[Mapper(PropertyNameMappingStrategy = PropertyNameMappingStrategy.CaseInsensitive)]
public partial class CarMapper
{
public partial CarDto ToDto(Car car);
}

public class Car
{
public string ModelName { get; set; }
}

public class CarDto
{
public string modelName { get; set; }
}