Use case ForAllOtherMembers?

See original GitHub issue

Consider two types with a large number of properties that will auto-map due to common names between them, say there are 30 such properties. However, you only want 10 of these to auto-map and the remaining 20 to be ignored and not mapped. Further, imagine that the remainder are to be auto-mapped from another type whose properties also line up; so, for that map, it is desired that the 20 will auto-map there, and the 10 here ignored there.

The accepted answer in discussion 4036 seems to say you should “manually / explicitly” map the 10 properties (to use @jbogard 's language from the discussion) in the first map, and the 20 in the second, even though all 30 satisfy the auto-mapping convention, in that source and destination property names are identical.

Since these subsets of properties fit the use-case for auto-mapping, there should be a way to tell AutoMapper to auto-map just the subset of properties but ignore the rest.

Suppose there was an overload of ForMember() that only took one argument, a selector for the properties to auto-map. Something like, .ForMember(x=>new[]{x.Prop1, x.Prop2, ..., x.PropN}) , where you list the properties you want auto-mapped from the source, but you didn’t have to explicitly give the source properties in a MapFrom argument for each, since they all had the same names anyway, so should not have to be explicitly listed (back to manual mapping).

With a solution like this one, Ignore with ForAllOtherMembers seems to now have a use case, since for the 10-map, you need to ignore the remaining 20, and for the 20-map, you need to ignore the 10.

Issue Analytics

  • State:closed
  • Created a month ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, Aug 16, 2023

AutoMapper was designed for 95-100% similarity. Not 2/3. Even 90% would be the threshold of ditching AutoMapper for me.

0reactions
aguevaracommented, Aug 16, 2023

Merging multiple objects was not the point of the example, but ok. The request is to give a user a way to configure a “threshold of similarity” that makes using your library worthwhile in their given situation. For example, a 2/3 or 3/4 or 5/6 “similarity” (common property names) between types seems like something AutoMapper was designed for, and 2/3 is very manageable when the source type has, say, 6 properties with the same names as a target type where only 2 must be ignored and the rest are auto-mapped. But in the earlier example, if 20 must be mapped and 10 ignored, (all 30 have the same names), then it’s still 2/3 similarity but now “not what AutoMapper was designed for”. Confusing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replacement for Automapper's ForAllOtherMembers()
My problem is any usage I have of this worked just fine for over 3 years - sometimes I don't want properties mapped...
Read more >
Exception Due To Replacing ForAllOtherMembers With ...
ForAllOtherMembers (opt => opt. Ignore()) is not equivalent to MemberList. None exactly. It does not cover the case where members actually map.
Read more >
11.0 Upgrade Guide
When only used for validation, it can be replaced with MemberList.None . C# Indexers ( Item property)¶. These used to be ignored by...
Read more >
AutoMapper's Design Philosophy
It assumes that you want to flatten complex models into simple ones. All of these assumptions come from our original use case -...
Read more >
AutoMapper ForAllOtherMembers - Manish Tiwari - Medium
AutoMapper ForAllOtherMembers · Sign up to discover human stories that deepen your understanding of the world. · Free · Membership · Written by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found