-
I have two classes. Each class has an associated mapper class. Class A has a property of type Mapper generated for class "B" #nullable enable
namespace Mappers
{
public partial class IdWithConcurrencyTokenMapper
{
public partial Dto.IdWithConcurrencyTokenDto ToDto(Entities.IdWithConcurrencyToken entity)
{
var target = new Dto.IdWithConcurrencyTokenDto();
target.Id = entity.Id;
target.ConcurrencyToken = entity.ConcurrencyToken;
return target;
}
}
} Mapper generated for class "A" namespace Mappers
{
public partial class PushChangesResultMapper
{
public partial Dto.PushChangesResultDto ToDto(Sync.PushChangesResult entity)
{
var target = new Dto.PushChangesResultDto();
target.ModifiedDates = System.Linq.Enumerable.Select(entity.ModifiedDates, x => MapToIdWithConcurrencyTokenDto(x));
return target;
}
// This should be using the mapper that was defined manually
private Dto.IdWithConcurrencyTokenDto MapToIdWithConcurrencyTokenDto(Entities.IdWithConcurrencyToken source)
{
var target = new Dto.IdWithConcurrencyTokenDto();
target.Id = source.Id;
target.ConcurrencyToken = source.ConcurrencyToken;
return target;
}
}
} Ideally, class A would use the mapper defined for class B rather than create its own mapper. Aside from code bloat, this is an issue when class B has custom mapping logic. In my case, this essentially rules out using Mapperly as my class "B" has custom mapping logic. A further challenge here is that the documentation recommends creating a private mapping method that is invoked by a public method when requiring Before/After map functionality. Any solution to the above mentioned problem would need to know how to call that public method when it is mapping nested properties/fields. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Thank you for opening this discussion. I think what you are trying will work if you use the same Mapper class for mappings of class A and B or if the mapper for class B extends the mapper of class A. If you use separate mapper classes for mappings they don't now about each other. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the suggestion! That seems like it should work. The only issue
is for people who want to reduce coupling between their mappers, but in
most cases it should be fine.
…On Tue, Feb 28, 2023 at 12:54 AM Lars ***@***.***> wrote:
Thank you for opening this discussion. I think what you are trying will
work if you use the same Mapper class for mappings of class A and B or if
the mapper for class B extends the mapper of class A. If you use separate
mapper classes for mappings they don't now about each other.
—
Reply to this email directly, view it on GitHub
<#274 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAUFIAHARJA2NIMJA2LFO7LWZWOLTANCNFSM6AAAAAAVJ24MZA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Do you think implementing shared configurations is possible? Since this project is heavily inspired by mapstruct, I'm talking about this feature in particular: I guess, for now, a solution is simply:
|
Beta Was this translation helpful? Give feedback.
Thank you for opening this discussion. I think what you are trying will work if you use the same Mapper class for mappings of class A and B or if the mapper for class B extends the mapper of class A. If you use separate mapper classes for mappings they don't now about each other.