Permalink
Browse files

Refactored slightly, added another test to verify base behavior can b…

…e overridden by a derived type mapping.
  • Loading branch information...
1 parent e222335 commit 045a76ccd91cacdb2133a73cd35e1a6cdfe97402 @JakeGinnivan committed with jbogard Oct 26, 2010
Showing with 37 additions and 20 deletions.
  1. +23 −18 src/AutoMapper/ConfigurationStore.cs
  2. +14 −2 src/UnitTests/Bug/IncludedMappingShouldInheritBaseMappings.cs
@@ -183,20 +183,7 @@ public TypeMap CreateTypeMap(Type source, Type destination, string profileName)
typeMap.Profile = profileName;
typeMap.IgnorePropertiesStartingWith = _globalIgnore;
- foreach (var map in _typeMaps.Where(t => t.TypeHasBeenIncluded(source, destination)))
- {
- foreach (var mappedProperty in map.GetCustomPropertyMaps().Where(m => m.IsMapped()))
- {
- var existingMapping = typeMap.GetPropertyMaps()
- .SingleOrDefault(m =>
- m.DestinationProperty == mappedProperty.DestinationProperty &&
- !m.IsMapped());
- if (existingMapping == null)
- {
- typeMap.AddPropertyMap(mappedProperty);
- }
- }
- }
+ IncludeBaseMappings(source, destination, typeMap);
_typeMaps.Add(typeMap);
_typeMapCache[new TypePair(source, destination)] = typeMap;
@@ -205,10 +192,28 @@ public TypeMap CreateTypeMap(Type source, Type destination, string profileName)
}
return typeMap;
- }
-
-
- public IFormatterCtorExpression<TValueFormatter> AddFormatter<TValueFormatter>() where TValueFormatter : IValueFormatter
+ }
+
+ private void IncludeBaseMappings(Type source, Type destination, TypeMap typeMap)
+ {
+ foreach (var map in _typeMaps.Where(t => t.TypeHasBeenIncluded(source, destination)))
+ {
+ foreach (var mappedProperty in map.GetCustomPropertyMaps().Where(m => m.IsMapped()))
+ {
+ var existingMapping = typeMap.GetPropertyMaps()
+ .SingleOrDefault(m =>
+ m.DestinationProperty == mappedProperty.DestinationProperty &&
+ !m.IsMapped());
+ if (existingMapping == null)
+ {
+ typeMap.AddPropertyMap(mappedProperty);
+ }
+ }
+ }
+ }
+
+
+ public IFormatterCtorExpression<TValueFormatter> AddFormatter<TValueFormatter>() where TValueFormatter : IValueFormatter
{
return GetProfile(DefaultProfileName).AddFormatter<TValueFormatter>();
}
@@ -42,20 +42,32 @@ public void included_mapping_should_inherit_base_mappings_should_not_throw()
}
[Test]
- public void included_mapping_should_inherit_base_mappings()
+ public void more_specific_map_should_override_base_mapping()
{
Mapper.CreateMap<ModelObject, DtoObject>()
.ForMember(d => d.BaseString, m => m.MapFrom(s => s.DifferentBaseString))
.Include<ModelSubObject, DtoSubObject>();
Mapper.CreateMap<ModelSubObject, DtoSubObject>();
+ Mapper.AssertConfigurationIsValid();
+ }
+
+ [Test]
+ public void included_mapping_should_inherit_base_mappings()
+ {
+ Mapper.CreateMap<ModelObject, DtoObject>()
+ .ForMember(d => d.BaseString, m => m.MapFrom(s => s.DifferentBaseString))
+ .Include<ModelSubObject, DtoSubObject>();
+ Mapper.CreateMap<ModelSubObject, DtoSubObject>()
+ .ForMember(d=>d.BaseString, m=>m.UseValue("789"));
+
var dto = Mapper.Map<ModelSubObject, DtoSubObject>(new ModelSubObject
{
DifferentBaseString = "123",
SubString = "456"
});
- Assert.AreEqual("123", dto.BaseString);
+ Assert.AreEqual("789", dto.BaseString);
Assert.AreEqual("456", dto.SubString);
}

0 comments on commit 045a76c

Please sign in to comment.