I use AutoMapper to ensure all properties are accounted for between Request, Database, and DTO (outgoing data-transfer-objects).
I have request objects coming via the API, to patch an object in the Database.
[Fact]
public void MapRequestToRecord()
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.AllowNullDestinationValues = true;
cfg.CreateMap<SomeRequest, SomeRecord>()
// cannot access property-info here to check for nullability
.ForAllMembers(opt => opt.Condition((s, t, srcV, destV, ctx) => srcV != null));
});
configuration.AssertConfigurationIsValid();
var mapper = configuration.CreateMapper();
mapper.Map<SomeRecord>(new SomeRequest()).Count.Should().Be(2);
mapper.Map<SomeRecord>(new SomeRequest { Count = 3 }).Count.Should().Be(3);
}
private class SomeRequest
{
public int? Count { get; set; }
}
private class SomeRecord
{
public int Count { get; set; } = 2;
}
The srcV is converted to 0, which is understandable. But in the Condition method I would like to know which property I'm on (PropertyInfo), so I can at least use reflection to check if the original source-value was null.
Is it possible to add this (either as param or to the ResolutionContext)?
BTW: tested with AutoMapper 14.0.0
I use AutoMapper to ensure all properties are accounted for between Request, Database, and DTO (outgoing data-transfer-objects).
I have request objects coming via the API, to patch an object in the Database.
The srcV is converted to 0, which is understandable. But in the Condition method I would like to know which property I'm on (PropertyInfo), so I can at least use reflection to check if the original source-value was null.
Is it possible to add this (either as param or to the ResolutionContext)?
BTW: tested with AutoMapper 14.0.0