Include all members from a nested member
If a class has a nested member which is flattened without the member name prefix, each member needs a manual MapProperty attribute to be mapped correctly. This could be simplified a lot by including all members of a nested property by one configuration attribute.
Name proposal: MapNestedPropertiesAttribute(string memberName)
(probably MapNestedMembersAttribute would be better as this includes fields and does not only apply to properties, but for backward compatibility MapProperty is called what it is, therefore the name should match).
Example:
class Document { string Name; DocumentSettings Settings;}
class DocumentSettings { string Locale; string CurrencySymbol; }
class DocumentDto { string Name; string Locale; string CurrencySymbol; }
[Mapper]
partial class Mapper
{
[MapNestedProperties(nameof(Document.Settings))]
public partial DocumentDto Map(Document doc);
// instead of:
[MapProperty("Settings.Locale", "Locale")]
[MapProperty("Settings.CurrencySymbol", "CurrencySymbol")]
public partial DocumentDto Map(Document doc);
}
AutoMapper supports this via IncludeMembers. See https://github.com/riok/mapperly/discussions/447#discussioncomment-5952448.
How should it be used for other mappings? ie should it map Document.Settings.Locale -> DocumentDto.SettingsLocale
@TimothyMakkison good question, didn't thought about this. I think this should diagnostic unless explicitly mapped.
Should it support nested members? Something like:
[Mapper]
partial class Mapper
{
[MapNestedProperties("Settings.Region"))]
public partial DocumentDto Map(Document doc);
// instead of:
[MapProperty("Settings.Locale", "Locale")]
[MapProperty("Settings.CurrencySymbol", "CurrencySymbol")]
public partial DocumentDto Map(Document doc);
}
@TimothyMakkison intuitively I'd say it should support nested members.
I am interested in this feature. Can I help?
Checkout #598, looks like they tried to add this feature but never completed it
Hi, what is the progress on this issue? This is a really useful and necessary feature.
@mindotale nothing new, there was an PR https://github.com/riok/mapperly/pull/587 by @zeidoo which was never completed. We do have this feature on our roadmap, but it currently isn‘t high priority. If it is for you, feel free to contribute or alternatively you could sponsor the project / this feature 😉
:tada: This issue has been resolved in version 3.6.0-next.1 :tada:
The release is available on:
- GitHub release
-
v3.6.0-next.1
Your semantic-release bot :package::rocket:
:tada: This issue has been resolved in version 3.6.0 :tada:
The release is available on:
- GitHub release
-
v3.6.0
Your semantic-release bot :package::rocket: