Closed luczito closed 4 months ago
I have created an example that works. You have 2 issues. In the constructor when you have foo = foo; you are not doing any assignment. You would have to do this.foo = foo. Typically properties would be capitalized and then Foo = foo would be doing an assignment. The second issue is that you have a parameterless constructor so this constructor is used in the mapping.
using Mapster;
var domain = new Domain { Foo = 1, Bar = 2, Baz = 3 };
var derivedDto = domain.Adapt<DerivedDto>();
Console.WriteLine("Foo:{0} Bar:{1} Baz:{2}", derivedDto.Foo, derivedDto.Bar, derivedDto.Baz);
public class MapsterConfiguration : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<Domain, DerivedDto>()
.MapToConstructor(true);
}
}
class BaseDto
{
public int Foo { get; private set; }
public int Bar { get; set; }
public BaseDto(int foo, int bar)
{
Foo = foo;
Bar = bar;
}
}
class DerivedDto : BaseDto
{
public int Baz { get; set; }
public DerivedDto(int baz, int foo, int bar) : base(foo, bar)
{
Baz = baz;
}
}
class Domain
{
public int Foo { get; init; }
public int Bar { get; init; }
public int Baz { get; init; }
}
I have an issue where a base class property with private setters are mapped to "null" when mapping a derived class.
I have the following setup:
And the same domain types but with public setters. When mapping:
baz and bar gets assigned but foo is always null, i've tried defining a mapping config as:
But this does not change the result. Is this an issue or am I missing something from the docs regarding mapping to private setters?