Initializer Conversion #7393
Replies: 2 comments
-
We also use AutoMapper and I can agree that sometimes there's a little too much "magic" going on that isn't obvious until runtime. Debugging stinks and things like required properties don't work the way you'd want them to. That said, this feels like too much of a niche case. Sure a lot of folks do object mapping, and we'd definitely benefit from the feature... to a point. We have a lot of customized mapping logic that I'm not sure a language feature could support in the general case. AM also supports projecting expressions, and I doubt a language feature would include that, meaning you'd still need some other tool. On the one hand it somewhat feels like an extension of We've started looking at Source-generated mapping logic. There's libraries like Mapperly that look like they can get the job done pretty well while supporting a lot of the features you yearn for. In my head I can hear Cyrus and others saying "use a source generator" that can be customized to your needs. I feel like that's where this request will ultimately land and I don't think I disagree. |
Beta Was this translation helpful? Give feedback.
-
It's been a few years since I posted this suggestion, but it keeps coming back to me. I've been preferring initializers over constructors for POCO objects, and using The problem is, if you use something like AutoMapper, and you map one object type to another, where you want to ignore certain properties in the destination because they will be assigned independently from the object mapper, well that prevents you from using The proposal above really helps in this regard, because I'm able to make async calls and perform assignments, all as part of initialization code for my object, without needing to do a bunch of other boilerplate code around it.
I looked at Mapperly again, and I still find shortcomings, particularly because I can't use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We use AutoMapper at work, and while the value in the simplified mappings is easy to see, I'm not at all fond of the implementation which leaves error discovery to runtime, so I started tinkering with how mapping functionality could be added to the C# language itself, and I came up with a new Initializer Conversion syntax.
The use case for this is simple: provide a way to convert (map) between two types that do not have a supported converter, using inline initializers.
Here's an example that illustrates the syntax I'd like to propose:
For the last line of the initializer, I had also considered
_ = auto,
(newauto
keyword) or_ = (t => _)
as a way to indicate that remaining properties should be automatically mapped from source properties with the same name. I'd like to hear other ideas from anyone who doesn't like_ = _
as a way to indicate that remaining mapping should be done automatically.If you only wanted to map a few properties, and leave everything else as default, you could do this instead:
If your properties all map by name, you would simply define the Initializer Conversion like this:
Initializer Conversion mappings can be reused simply through methods.
The value in such a language feature as I see it would be:
new
with initializers).This new syntax significantly simplifies object mappings in C# like AutoMapper does without losing the benefits of compilation errors.
Beta Was this translation helpful? Give feedback.
All reactions