Skip to content

Commit 1807656

Browse files
Merge pull request #41 from fubar-coder/fix-context-passthrough
Pass context to mapper to make PreserveReferences work
2 parents e54297e + 3fc0aff commit 1807656

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/AutoMapper.Collection.Tests/MapCollectionWithEquality.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ public void Should_Be_Instanced_Based()
8484
Mapper.Map(dtos, items.ToList()).Should().NotContain(items.First());
8585
}
8686

87+
public void Parent_Should_Be_Same_As_Root_Object()
88+
{
89+
var mapper = new MapperConfiguration(
90+
cfg =>
91+
{
92+
cfg.AddCollectionMappers();
93+
cfg.CreateMap<ThingWithCollection, ThingWithCollection>()
94+
.PreserveReferences();
95+
cfg.CreateMap<ThingCollectionItem, ThingCollectionItem>()
96+
.EqualityComparison((src, dst) => src.ID == dst.ID)
97+
.PreserveReferences();
98+
})
99+
.CreateMapper();
100+
101+
var root = new ThingWithCollection()
102+
{
103+
Children = new List<ThingCollectionItem>()
104+
};
105+
root.Children.Add(new ThingCollectionItem() { ID = 1, Parent = root });
106+
107+
var target = new ThingWithCollection() { Children = new List<ThingCollectionItem>() };
108+
mapper.Map(root, target).Should().Be(target);
109+
110+
target.Children.Count.Should().Be(1);
111+
target.Children.Single().Parent.Should().Be(target);
112+
}
113+
87114
public class Thing
88115
{
89116
public int ID { get; set; }
@@ -96,5 +123,16 @@ public class ThingDto
96123
public int ID { get; set; }
97124
public string Title { get; set; }
98125
}
126+
127+
public class ThingWithCollection
128+
{
129+
public ICollection<ThingCollectionItem> Children { get; set; }
130+
}
131+
132+
public class ThingCollectionItem
133+
{
134+
public int ID { get; set; }
135+
public ThingWithCollection Parent { get; set; }
136+
}
99137
}
100138
}

src/AutoMapper.Collection/Mappers/EquivalentExpressionAddRemoveCollectionMapper.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,14 @@ public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationI
2929
foreach (var keypair in compareSourceToDestination)
3030
{
3131
if (keypair.Value == null)
32-
destination.Add(context.Mapper.Map<TDestinationItem>(keypair.Key, opts => CopyOptions(context.Options, opts)));
32+
destination.Add((TDestinationItem) context.Mapper.Map(keypair.Key, null, typeof(TSourceItem), typeof(TDestinationItem), context));
3333
else
34-
context.Mapper.Map(keypair.Key, keypair.Value, opts => CopyOptions(context.Options, opts));
34+
context.Mapper.Map(keypair.Key, keypair.Value, context);
3535
}
3636

3737
return destination;
3838
}
3939

40-
private static void CopyOptions(IMappingOperationOptions source, IMappingOperationOptions dest)
41-
{
42-
foreach (var item in source.Items)
43-
{
44-
dest.Items[item.Key] = item.Value;
45-
}
46-
}
47-
4840
private static readonly MethodInfo MapMethodInfo = typeof(EquivalentExpressionAddRemoveCollectionMapper).GetRuntimeMethods().First(_ => _.IsStatic);
4941

5042
public bool IsMatch(TypePair typePair)

0 commit comments

Comments
 (0)