Created
November 14, 2012 02:30
-
-
Save mightymuke/4069909 to your computer and use it in GitHub Desktop.
AutoMapper AllowNullCollections Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace NullCollectionIssue | |
{ | |
using AutoMapper; | |
public class ProfileOne : Profile | |
{ | |
public override string ProfileName | |
{ | |
get | |
{ | |
return "ProfileOne"; | |
} | |
} | |
protected override void Configure() | |
{ | |
AllowNullCollections = true; | |
CreateMap<SourceProfileOne, DestProfileOne>(); | |
} | |
} | |
public class ProfileTwo : Profile | |
{ | |
public override string ProfileName | |
{ | |
get | |
{ | |
return "ProfileTwo"; | |
} | |
} | |
protected override void Configure() | |
{ | |
CreateMap<SourceProfileTwo, DestProfileTwo>(); | |
} | |
} | |
public static class AutoMapperConfigurator | |
{ | |
public static void Configure() | |
{ | |
Mapper.Initialize(x => | |
{ | |
x.AddProfile<ProfileOne>(); | |
x.AddProfile<ProfileTwo>(); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace NullCollectionIssue | |
{ | |
using AutoMapper; | |
using NUnit.Framework; | |
[TestFixture] | |
public class MappingTests | |
{ | |
[Test] | |
public void AutoMapper_Configuration_IsValid() | |
{ | |
AutoMapperConfigurator.Configure(); | |
Mapper.AssertConfigurationIsValid(); | |
} | |
[Test] | |
public void AutoMapper_ProfileOne_AllowsNullCollections() | |
{ | |
AutoMapperConfigurator.Configure(); | |
Mapper.AssertConfigurationIsValid(); | |
var source = new SourceProfileOne { Stuff = null }; | |
var dest = Mapper.Map<SourceProfileOne, DestProfileOne>(source); | |
Assert.That(dest, Is.Not.Null); | |
Assert.That(dest.Stuff, Is.Null); | |
} | |
[Test] | |
public void AutoMapper_ProfileTwo_DoesntAllowNullCollections() | |
{ | |
AutoMapperConfigurator.Configure(); | |
Mapper.AssertConfigurationIsValid(); | |
var source = new SourceProfileTwo { Stuff = null }; | |
var dest = Mapper.Map<SourceProfileTwo, DestProfileTwo>(source); | |
Assert.That(dest, Is.Not.Null); | |
Assert.That(dest.Stuff, Is.Not.Null); | |
Assert.That(dest.Stuff, Is.Empty); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace NullCollectionIssue | |
{ | |
using System.Collections.Generic; | |
public class SourceProfileOne | |
{ | |
public ICollection<string> Stuff { get; set; } | |
} | |
public class DestProfileOne | |
{ | |
public ICollection<string> Stuff { get; set; } | |
} | |
public class SourceProfileTwo | |
{ | |
public ICollection<string> Stuff { get; set; } | |
} | |
public class DestProfileTwo | |
{ | |
public ICollection<string> Stuff { get; set; } | |
} | |
} |
Thanks for sharing. I just set AllowNullCollections = true inside the profile constructor and worked as well.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the StackOverflow question - this behaviour was because I was calling the static Mapper.CreateMap in the configure method. Changed it to the instance method, and it worked.