Skip to content

Commit

Permalink
Merge pull request #2438 from lbargaoanu/ObservableCollection
Browse files Browse the repository at this point in the history
We don't need the property map when mapping the item; for nested coll…
  • Loading branch information
jbogard committed Dec 1, 2017
2 parents 096e58a + 7f7abb3 commit 605941b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/AutoMapper/Mappers/ArrayMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override Expression MapExpression(IConfigurationProvider configurationPro
var sourceElementType = ElementTypeHelper.GetElementType(sourceExpression.Type);
var destElementType = ElementTypeHelper.GetElementType(destExpression.Type);

var itemExpr = MapItemExpr(configurationProvider, profileMap, propertyMap, sourceExpression.Type, destExpression.Type, contextExpression, out ParameterExpression itemParam);
var itemExpr = MapItemExpr(configurationProvider, profileMap, sourceExpression.Type, destExpression.Type, contextExpression, out ParameterExpression itemParam);

//var count = source.Count();
//var array = new TDestination[count];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace AutoMapper.Mappers.Internal
public static class CollectionMapperExpressionFactory
{
public delegate Expression MapItem(IConfigurationProvider configurationProvider, ProfileMap profileMap,
PropertyMap propertyMap, Type sourceType, Type destType, Expression contextParam,
Type sourceType, Type destType, Expression contextParam,
out ParameterExpression itemParam);

public static Expression MapCollectionExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression, Type ifInterfaceType, MapItem mapItem)
Expand All @@ -24,7 +24,7 @@ public static Expression MapCollectionExpression(IConfigurationProvider configur
var newExpression = Variable(passedDestination.Type, "collectionDestination");
var sourceElementType = ElementTypeHelper.GetElementType(sourceExpression.Type);

var itemExpr = mapItem(configurationProvider, profileMap, propertyMap, sourceExpression.Type, passedDestination.Type,
var itemExpr = mapItem(configurationProvider, profileMap, sourceExpression.Type, passedDestination.Type,
contextExpression, out ParameterExpression itemParam);

var destinationElementType = itemExpr.Type;
Expand Down Expand Up @@ -86,20 +86,19 @@ private static Expression NewExpr(this Type baseType, Type ifInterfaceType)
return newExpr;
}

public static Expression MapItemExpr(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Type sourceType, Type destType, Expression contextParam, out ParameterExpression itemParam)
public static Expression MapItemExpr(IConfigurationProvider configurationProvider, ProfileMap profileMap, Type sourceType, Type destType, Expression contextParam, out ParameterExpression itemParam)
{
var sourceElementType = ElementTypeHelper.GetElementType(sourceType);
var destElementType = ElementTypeHelper.GetElementType(destType);
itemParam = Parameter(sourceElementType, "item");

var typePair = new TypePair(sourceElementType, destElementType);

var itemExpr = MapExpression(configurationProvider, profileMap, typePair, itemParam, contextParam,
propertyMap);
var itemExpr = MapExpression(configurationProvider, profileMap, typePair, itemParam, contextParam);
return ToType(itemExpr, destElementType);
}

public static Expression MapKeyPairValueExpr(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Type sourceType, Type destType, Expression contextParam, out ParameterExpression itemParam)
public static Expression MapKeyPairValueExpr(IConfigurationProvider configurationProvider, ProfileMap profileMap, Type sourceType, Type destType, Expression contextParam, out ParameterExpression itemParam)
{
var sourceElementTypes = ElementTypeHelper.GetElementTypes(sourceType, ElementTypeFlags.BreakKeyValuePair);
var destElementTypes = ElementTypeHelper.GetElementTypes(destType, ElementTypeFlags.BreakKeyValuePair);
Expand All @@ -112,9 +111,9 @@ public static Expression MapKeyPairValueExpr(IConfigurationProvider configuratio
var destElementType = typeof(KeyValuePair<,>).MakeGenericType(destElementTypes);

var keyExpr = MapExpression(configurationProvider, profileMap, typePairKey,
Property(itemParam, "Key"), contextParam, propertyMap);
Property(itemParam, "Key"), contextParam);
var valueExpr = MapExpression(configurationProvider, profileMap, typePairValue,
Property(itemParam, "Value"), contextParam, propertyMap);
Property(itemParam, "Value"), contextParam);
var keyPair = New(destElementType.GetDeclaredConstructors().First(), keyExpr, valueExpr);
return keyPair;
}
Expand Down
35 changes: 35 additions & 0 deletions src/UnitTests/CollectionMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@

namespace AutoMapper.UnitTests
{
public class When_mapping_to_existing_observable_collection : AutoMapperSpecBase
{
class CollectionHolder
{
public CollectionHolder()
{
Observable = new ObservableCollection<List<int>>();
}

public ObservableCollection<List<int>> Observable { get; set; }
}

class CollectionHolderDto
{
public CollectionHolderDto()
{
Observable = new List<List<int>>();
}

public List<List<int>> Observable { get; set; }
}

protected override MapperConfiguration Configuration => new MapperConfiguration(cfg => cfg.CreateMap<CollectionHolderDto, CollectionHolder>().ForMember(a => a.Observable, opt => opt.UseDestinationValue()));

[Fact]
public void Should_map_ok()
{
var ch = new CollectionHolderDto();
var list = new List<int>{ 5, 6 };
ch.Observable.Add(list);
var mapped = Mapper.Map<CollectionHolder>(ch);
mapped.Observable.Single().ShouldBe(list);
}
}

public class When_mapping_to_existing_collection_typed_as_IEnumerable : AutoMapperSpecBase
{
protected override MapperConfiguration Configuration => new MapperConfiguration(_=>{ });
Expand Down

0 comments on commit 605941b

Please sign in to comment.