Wednesday, March 14, 2012

Automapper Array mapping issue

For a project we are using Automapper to map our domain model to our WCF DataContracts.

Last week a bug was logged that a DTO with an array property was set to a zero-length array, although the source instance has the property set to null. We expected that the destination value would also be null. As all our DataContracts are validated against our XSD schemas, an error was thrown.

On the GitHub Automapper site we found that this was already reported as an issue. As a result a new configuration option was added.

If you didn’t download the latest version from the GitHub site but are using an official release, you can get around the issue by using the following code:

Mapper.CreateMap<Order, OrderDTO>()
.ForMember(o => o.OrderLines, opt => opt.ResolveUsing(o => o.OrderLines== null ? null: o.OrderLines));

No comments: