Skip to content

Commit 146be21

Browse files
authored
Merge pull request #2407 from AutoMapper/MapperReset
Add Mapper.Reset to reset static mapping
2 parents 08fa99f + c18888f commit 146be21

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

docs/Configuration.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,15 @@ Mapper.Configuration.CompileMappings();
217217
```
218218

219219
For a few hundred mappings, this may take a couple of seconds.
220+
221+
## Resetting static mapping configuration
222+
223+
The static `Mapper.Initialize` is intended to be called only once. To reset the static mapping configuration (for example, at the start of tests):
224+
225+
```c#
226+
Mapper.Reset();
227+
228+
Mapper.Initialize(cfg => { /* configure again */ });
229+
```
230+
231+
`Reset` should not be used in production code. It is intended to support testing scenarios only.

src/AutoMapper/Mapper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public static void Initialize(MapperConfigurationExpression config)
5353
Instance = new Mapper(Configuration);
5454
}
5555

56+
/// <summary>
57+
/// Resets the mapper configuration. Not intended for production use, but for testing scenarios.
58+
/// </summary>
59+
public static void Reset()
60+
{
61+
_configuration = null;
62+
_instance = null;
63+
}
64+
5665
/// <summary>
5766
/// Execute a mapping from the source object to a new destination object.
5867
/// The source type is inferred from the source object.

src/UnitTests/StaticMapping.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using QueryableExtensions;
77
using System;
88

9+
#if !NET40
10+
[Collection(nameof(StaticMapping))]
11+
#endif
912
public class StaticMapping
1013
{
1114
public class ModelObject
@@ -62,20 +65,26 @@ public class Dest
6265
public int Value { get; set; }
6366
}
6467

65-
static StaticMapping()
68+
private void InitializeMapping()
6669
{
67-
Mapper.Initialize(cfg =>
70+
lock (this)
6871
{
69-
cfg.CreateMap<Source, Dest>();
70-
cfg.CreateMap<ModelObject, ModelDto>();
71-
cfg.CreateMap<ModelObject2, ModelDto2>();
72-
cfg.CreateMap<ModelObject3, ModelDto3>(MemberList.Source);
73-
});
72+
Mapper.Reset();
73+
Mapper.Initialize(cfg =>
74+
{
75+
cfg.CreateMap<Source, Dest>();
76+
cfg.CreateMap<ModelObject, ModelDto>();
77+
cfg.CreateMap<ModelObject2, ModelDto2>();
78+
cfg.CreateMap<ModelObject3, ModelDto3>(MemberList.Source);
79+
});
80+
}
7481
}
7582

7683
[Fact]
7784
public void Can_map_statically()
7885
{
86+
InitializeMapping();
87+
7988
var source = new Source {Value = 5};
8089

8190
var dest = Mapper.Map<Source, Dest>(source);
@@ -86,6 +95,8 @@ public void Can_map_statically()
8695
[Fact]
8796
public void Can_project_statically()
8897
{
98+
InitializeMapping();
99+
89100
var source = new Source {Value = 5};
90101
var sources = new[] {source}.AsQueryable();
91102

@@ -98,13 +109,33 @@ public void Can_project_statically()
98109
[Fact]
99110
public void Should_fail_a_configuration_check()
100111
{
112+
InitializeMapping();
113+
101114
typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid);
102115
}
103116

104117
[Fact]
105118
public void Should_throw_when_initializing_twice()
106119
{
107-
typeof(InvalidOperationException).ShouldBeThrownBy(() => Mapper.Initialize(_ => { }));
120+
typeof(InvalidOperationException).ShouldBeThrownBy(() =>
121+
{
122+
Mapper.Initialize(_ => { });
123+
Mapper.Initialize(_ => { });
124+
});
108125
}
126+
127+
[Fact]
128+
public void Should_not_throw_when_resetting()
129+
{
130+
var action = new Action(() =>
131+
{
132+
Mapper.Reset();
133+
Mapper.Initialize(cfg => { });
134+
Mapper.Reset();
135+
Mapper.Initialize(cfg => { });
136+
});
137+
action.ShouldNotThrow();
138+
}
139+
109140
}
110141
}

0 commit comments

Comments
 (0)