Skip to main content

Existing target object

If an existing object instance should be used as target, you can define the mapping method as void with the target as second parameter:

Mapper declaration
[Mapper]
public partial class CarMapper
{
public partial void CarToCarDto(Car car, CarDto dto);
}
Mapper usage
var mapper = new CarMapper();
var car = new Car { NumberOfSeats = 10, ... };
var dto = new CarDto();

mapper.CarToCarDto(car, dto);
dto.NumberOfSeats.Should().Be(10);

Merge objects

To merge two objects together, AllowNullPropertyAssignment can be set to false. This ignores all properties on the source with a null value.

[Mapper(AllowNullPropertyAssignment = false)]
static partial class FruitMapper
{
public static partial void ApplyUpdate(FruitUpdate update, Fruit fruit);
}

class Fruit { public required string Name { get; set; } public required string Color { get; set; } }
record FruitUpdate(string? Name, string? Color);

See also null value handling.