TypeConverter - Option to not reassign same reference to destination property.

18 views
Skip to first unread message

cjm.ai...@gmail.com

unread,
Jun 3, 2017, 5:52:51 PM6/3/17
to AutoMapper-users
Maybe this option is already somewhere in AutoMapper, but I can't find it.

When a TypeConverter returns the same destination object (as the one that was passed in to Convert), it'd be nice if by default (and maybe have an option) it wasn't reassigned to the destination property. That way you wouldn't invoke the setter on the destination property - which might cause problems (like if there is no setter, or if you're working with weird things like EF Proxies), or, in general, which might trigger some unwanted behavior.

This might be an issue in other places in AutoMapper besides TypeConverters, but I haven't really thought about it, and I don't have that much experience with AutoMapper.

Jimmy Bogard

unread,
Jun 4, 2017, 9:35:15 AM6/4/17
to automapp...@googlegroups.com
This really doesn't have anything to do with type converters, that's just one of many mapping strategies before assigning a value.

Whether or not to assign a value is something else entirely. 

What are you trying to do?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cjm.ai...@gmail.com

unread,
Jun 5, 2017, 10:06:56 AM6/5/17
to AutoMapper-users
I'm working with Entity Framework entities and I'd rather not set the collections. Even though I haven't run into any problems yet, they really shouldn't be set.   Also, this might be a problem if the dest object's property (like a collection) doesn't have a setter.

Is there any option for changing this behavior?

Jimmy Bogard

unread,
Jun 5, 2017, 1:28:17 PM6/5/17
to automapp...@googlegroups.com
Have you looked at UseDestinationValue?

cjm.ai...@gmail.com

unread,
Jun 8, 2017, 10:36:13 PM6/8/17
to AutoMapper-users
I don't see how to use UseDestinationValue with a TypeConverter.

For example, if you do this:


                this.CreateMap<XSource, XDest>().ForMember(d => d.Ys, ops => ops.UseDestinationValue()); // d.Ys is a ICollection<YDest>
                
                this.CreateMap(typeof(ICollection<YSource>), typeof(ICollection<YDest>))
                    .ConvertUsing(typeof(CollectionConverter<YSource, YDest>));

then the UseDestinationValue option is ignored, and the value returned by the CollectionConverter (an ICollection<YDest>) is still set to the property XDest.Ys.

cjm.ai...@gmail.com

unread,
Jun 8, 2017, 10:37:56 PM6/8/17
to AutoMapper-users, cjm.ai...@gmail.com
I see you have something called Automapper.Collections which says something about not setting the destination's collection property. But this question is just a more general case.

Jimmy Bogard

unread,
Jun 9, 2017, 4:04:24 AM6/9/17
to automapper-users
What question? Type converters are fundamentally different, they completely override a mapping function. That mapping function is how to resolve a value, AutoMapper has separate logic in determining how to *set* the value. AutoMapper.Collections combines these two together into one singular behavior.

I kept them separate inside AutoMapper because frankly, I don't reverse map and so have never needed any of those features related to reverse mapping.

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.

cjm.ai...@gmail.com

unread,
Jun 9, 2017, 3:01:01 PM6/9/17
to AutoMapper-users
By "my question" I mean "the topic of this post".

Right, I see that those two things (TypeConverter & "don't set the dest property") are separate things. I'm just asking if, when using a TypeConverter, there is an option to not set the dest property if the reference returned from the TypeConverter is the same (as what came into the TypeConverter). It doesn't look like there is that option. I see that AutoMapper.Collections does not set the dest property, but that is limited to collections and not just any property.

Jimmy Bogard

unread,
Jun 9, 2017, 5:40:37 PM6/9/17
to automapper-users
It's a trick the package is doing by taking the existing value in and returning it.

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages