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]
public partial class CarMapper
{
public partial void UpdateCarDto(Car car, CarDto dto);
}

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.

The MappingTarget attribute allows setting the first method parameter as mapping target:

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

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

See also extension methods.