Skip to content

Commit 6873103

Browse files
committed
Resetting expression cache on mapper.reset; fixes #614
1 parent d3a1f54 commit 6873103

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

src/AutoMapper/Internal/ConcurrentDictionaryFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public TValue this[TKey key]
4444
public bool TryRemove(TKey key, out TValue value)
4545
{
4646
return _dictionary.TryRemove(key, out value);
47-
}
47+
}
48+
49+
public void Clear()
50+
{
51+
_dictionary.Clear();
52+
}
4853
}
4954
}
5055
}

src/AutoMapper/Internal/DictionaryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public bool TryRemove(TKey key, out TValue value)
8787
return true;
8888
}
8989
}
90+
91+
public void Clear()
92+
{
93+
_dictionary.Clear();
94+
}
9095
}
9196
}
9297
}

src/AutoMapper/Internal/IDictionary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Func<TKey, TValue> valueFactory
4848
);
4949

5050
TValue this[TKey key] { get; set; }
51-
bool TryRemove(TKey key, out TValue value);
51+
bool TryRemove(TKey key, out TValue value);
52+
void Clear();
5253
}
5354

5455
public interface INullableConverter

src/AutoMapper/Mapper.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
namespace AutoMapper
55
{
6-
using Internal;
7-
6+
using Internal;
7+
using QueryableExtensions;
8+
89
/// <summary>
910
/// Main entry point for AutoMapper, for both creating maps and performing maps.
1011
/// </summary>
@@ -438,7 +439,8 @@ public static void AssertConfigurationIsValid(string profileName)
438439
/// </summary>
439440
public static void Reset()
440441
{
441-
MapperRegistry.Reset();
442+
MapperRegistry.Reset();
443+
Extensions.ClearExpressionCache();
442444
_configuration = LazyFactory.Create(_configurationInit);
443445
_mappingEngine = LazyFactory.Create(_mappingEngineInit);
444446
}

src/AutoMapper/QueryableExtensions/Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ private static readonly Internal.IDictionary<ExpressionRequest, LambdaExpression
3232
new StringExpressionBinder(),
3333
};
3434

35+
public static void ClearExpressionCache()
36+
{
37+
_expressionCache.Clear();
38+
}
39+
3540
/// <summary>
3641
/// Create an expression tree representing a mapping from the <typeparamref name="TSource"/> type to <typeparamref name="TDestination"/> type
3742
/// Includes flattening and expressions inside MapFrom member configuration

src/UnitTests/Projection/ExplicitValues.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,45 @@ public void Should_substitute_value()
3939
_dests[0].Value.ShouldEqual(5);
4040
}
4141
}
42+
43+
public class ExplicitValues_ForReset : AutoMapperSpecBase
44+
{
45+
private List<Dest> _dests;
46+
47+
public class Source
48+
{
49+
public int Value { get; set; }
50+
}
51+
52+
public class Dest
53+
{
54+
public int Value { get; set; }
55+
}
56+
57+
protected override void Establish_context()
58+
{
59+
Mapper.CreateMap<Source, Dest>()
60+
.ForMember(dest => dest.Value, opt => opt.UseValue(5));
61+
62+
new[] { new Source { Value = 10 } }.AsQueryable().Project().To<Dest>().ToList();
63+
64+
Mapper.Reset();
65+
66+
Mapper.CreateMap<Source, Dest>()
67+
.ForMember(dest => dest.Value, opt => opt.UseValue(10));
68+
}
69+
70+
protected override void Because_of()
71+
{
72+
var source = new[] { new Source { Value = 10 } }.AsQueryable();
73+
74+
_dests = source.Project().To<Dest>().ToList();
75+
}
76+
77+
[Fact]
78+
public void Should_substitute_value()
79+
{
80+
_dests[0].Value.ShouldEqual(10);
81+
}
82+
}
4283
}

0 commit comments

Comments
 (0)