Replies: 2 comments 4 replies
-
Thank you! This is currently not supported. However, using inherited methods from a base class is supported (inheriting methods with implementation bodies are already supported, partial / generated base class mapping methods are planned, see #38). The following works with the current version of Mapperly: class DateMapper
{
protected DateOnly DateTimeToDateOnly(DateTime dt) => DateOnly.FromDateTime(dt);
}
[Mapper]
partial class DtoMapper : DateMapper
{
public partial CarDto ToDto(Car car);
} In this sample, when the [Mapper]
class DateMapper
{
public partial DateDto ToDateDto(DateTime dt);
}
[Mapper]
partial class DtoMapper : DateMapper
{
private readonly DateMapper _dateMapper = new();
public partial CarDto ToDto(Car car);
private DateDto ToDateDto(DateTime dt) => _dateMapper.ToDateDto(dt);
} or split your mapper into several files with the partial class feature. |
Beta Was this translation helpful? Give feedback.
-
Can I ask what makes this feature complex? From the 10 000 foot view this looks straightforward with all the building boxes at hand. All what seems to be necessary is to add every method from the Let's consider this. BTW the referenced mappers do not need [Mapper]
[UseMapper(typeof(SomeMapper), typeof(OtherMapper))]
[InjectMapper(typeof(ExtraMapper), typeof(CoolMapper))]
internal sealed partial class Foo {
public partial Dst Mapping(Src src);
}
// would generate
internal sealed partial class Foo {
private readonly ExtraMapper injectedMapper1;
private readonly CoolMapper injectedMapper2;
public Foo(ExtraMapper injectedMapper1, CoolMapper injectedMapper2) {
this.injectedMapper1 = injectedMapper1;
this.injectedMapper2 = injectedMapper2;
}
public partial Dst Mapping(Src src) {
var externalMapper1 = new SomeMapper();
var dst = new Dst() { A = src.A, B = src.B };
dst.Foo = OtherMapper.Mapping(src.Foo); // static method mapping
dst.Bar = externalMapper1.Mapping(src.Bar);
dst.Baz = externalMapper1.Mapping(src.Baz); // mapper re-used
dst.Bax = this.injectedMapper1.Mapping(src.Bax);
dst.Ban = this.injectedMapper2.Mapping(src.Ban);
return dst;
}
} |
Beta Was this translation helpful? Give feedback.
-
I just came here to say thank you sir, I was waiting for this so much!
I have one question though - do you plan to support dependency between mappers? For example I have a
DateConversionMapper
with, well, date conversions and then I want to create domain specific mapper that relies on the mappings fromDateConversionMapper
. MapStruct supports this byuses
attribute property: https://mapstruct.org/documentation/stable/reference/html/#invoking-other-mappersBeta Was this translation helpful? Give feedback.
All reactions