Can AutoMappers AllowNullCollections setting be constrained to a profile?

118 views
Skip to first unread message

MightyMuke

unread,
Nov 10, 2012, 3:07:49 AM11/10/12
to automapp...@googlegroups.com
Hi,

I have an unanswered question over at stack overflow that has earned me the tumbleweed badge: http://stackoverflow.com/questions/13186463/can-automappers-allownullcollections-setting-be-constrained-to-a-profile
I was wondering if anyone here was able to answer it? If you prefer to answer it on stack overflow, I can give you the kudos for it.

Thanks for your help.

A copy and paste of the question:

My MVC application essentially has two levels of mapping (simplified for this question):

RepositoryObject <-> Entity <-> ViewModel

We have created two profiles, each handling the configuration for the appropriate mapping level.

The "RepositoryObjects" are eventually serialised to XML and used in REST web services. The problem we found was that an empty collection in the RepositoryObject would serialise down to an empty element in the XML, and this would cause an issue as the web service is either expecting no element, or an element containing data.

We were able to resolve this using the AllowNullCollections configuration setting. This will (of course) create a null collection rather than an empty collection, which then serialises fine.

However I'm not entirely comfortable having this as a global setting as because as Jimmy has pointed out, its not really best practice. I'm happy to have it in the RepositoryObject <-> Entity mapping because the RepositoryObjects are auto-generated (so are ugly anyway) and its very low-level in the application. But I'd prefer to not "corrupt" the Entity <-> ViewModel mapping if possible.

So, is it possible to configure this setting per profile?

Thanks.

Jimmy Bogard

unread,
Nov 12, 2012, 7:32:28 PM11/12/12
to automapp...@googlegroups.com
Yes, it can be constrained per profile (at least on 2.2). Is that not what you see?

Just make sure you use the base Profile property (and not through the Mapper class).

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/automapper-users/-/fG3koTLfYLoJ.
To post to this group, send email to automapp...@googlegroups.com.
To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/automapper-users?hl=en.

Mightymuke

unread,
Nov 13, 2012, 9:35:58 PM11/13/12
to automapp...@googlegroups.com
Hi,

It doesn't seem to be working, but it could simply be an issue with how I'm configuring it.

Here is an example of what I'm doing: https://gist.github.com/4069909

The tests AutoMapper_Configuration_IsValid and AutoMapper_ProfileTwo_DoesntAllowNullCollections pass, but the test AutoMapper_ProfileOne_AllowsNullCollections fails because dest.Stuff is not null.

Thanks,
MM.

Mightymuke

unread,
Nov 13, 2012, 10:44:44 PM11/13/12
to automapp...@googlegroups.com
@wal pointed out that I was using the static Mapper.CreateMap. I changed to the instance version and it worked like a charm.
Thanks.

Reply all
Reply to author
Forward
0 new messages