mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Include all members from a nested member

Open latonz opened this issue 2 years ago • 6 comments

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.

latonz avatar May 22 '23 14:05 latonz

How should it be used for other mappings? ie should it map Document.Settings.Locale -> DocumentDto.SettingsLocale

TimothyMakkison avatar Jun 09 '23 13:06 TimothyMakkison

@TimothyMakkison good question, didn't thought about this. I think this should diagnostic unless explicitly mapped.

latonz avatar Jun 09 '23 14:06 latonz

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 avatar Jun 14 '23 09:06 TimothyMakkison

@TimothyMakkison intuitively I'd say it should support nested members.

latonz avatar Jun 14 '23 11:06 latonz

I am interested in this feature. Can I help?

trejjam avatar Dec 13 '23 11:12 trejjam

Checkout #598, looks like they tried to add this feature but never completed it

TimothyMakkison avatar Dec 13 '23 12:12 TimothyMakkison

Hi, what is the progress on this issue? This is a really useful and necessary feature.

mindotale avatar Mar 27 '24 14:03 mindotale

@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 😉

latonz avatar Mar 27 '24 14:03 latonz

:tada: This issue has been resolved in version 3.6.0-next.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar May 03 '24 18:05 github-actions[bot]

:tada: This issue has been resolved in version 3.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jun 18 '24 20:06 github-actions[bot]