diff --git a/docs/Configuration.md b/docs/Configuration.md index 45918fec4a..64e87de3e6 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -217,3 +217,15 @@ Mapper.Configuration.CompileMappings(); ``` For a few hundred mappings, this may take a couple of seconds. + +## Resetting static mapping configuration + +The static `Mapper.Initialize` is intended to be called only once. To reset the static mapping configuration (for example, at the start of tests): + +```c# +Mapper.Reset(); + +Mapper.Initialize(cfg => { /* configure again */ }); +``` + +`Reset` should not be used in production code. It is intended to support testing scenarios only. \ No newline at end of file diff --git a/src/AutoMapper/Mapper.cs b/src/AutoMapper/Mapper.cs index 03f59d72b0..cc2f06b6d3 100644 --- a/src/AutoMapper/Mapper.cs +++ b/src/AutoMapper/Mapper.cs @@ -53,6 +53,15 @@ public static void Initialize(MapperConfigurationExpression config) Instance = new Mapper(Configuration); } + /// + /// Resets the mapper configuration. Not intended for production use, but for testing scenarios. + /// + public static void Reset() + { + _configuration = null; + _instance = null; + } + /// /// Execute a mapping from the source object to a new destination object. /// The source type is inferred from the source object. diff --git a/src/UnitTests/StaticMapping.cs b/src/UnitTests/StaticMapping.cs index d7ad015c63..803b7dccd6 100644 --- a/src/UnitTests/StaticMapping.cs +++ b/src/UnitTests/StaticMapping.cs @@ -6,6 +6,9 @@ using QueryableExtensions; using System; +#if !NET40 + [Collection(nameof(StaticMapping))] +#endif public class StaticMapping { public class ModelObject @@ -62,20 +65,26 @@ public class Dest public int Value { get; set; } } - static StaticMapping() + private void InitializeMapping() { - Mapper.Initialize(cfg => + lock (this) { - cfg.CreateMap(); - cfg.CreateMap(); - cfg.CreateMap(); - cfg.CreateMap(MemberList.Source); - }); + Mapper.Reset(); + Mapper.Initialize(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(MemberList.Source); + }); + } } [Fact] public void Can_map_statically() { + InitializeMapping(); + var source = new Source {Value = 5}; var dest = Mapper.Map(source); @@ -86,6 +95,8 @@ public void Can_map_statically() [Fact] public void Can_project_statically() { + InitializeMapping(); + var source = new Source {Value = 5}; var sources = new[] {source}.AsQueryable(); @@ -98,13 +109,33 @@ public void Can_project_statically() [Fact] public void Should_fail_a_configuration_check() { + InitializeMapping(); + typeof(AutoMapperConfigurationException).ShouldBeThrownBy(Mapper.AssertConfigurationIsValid); } [Fact] public void Should_throw_when_initializing_twice() { - typeof(InvalidOperationException).ShouldBeThrownBy(() => Mapper.Initialize(_ => { })); + typeof(InvalidOperationException).ShouldBeThrownBy(() => + { + Mapper.Initialize(_ => { }); + Mapper.Initialize(_ => { }); + }); } + + [Fact] + public void Should_not_throw_when_resetting() + { + var action = new Action(() => + { + Mapper.Reset(); + Mapper.Initialize(cfg => { }); + Mapper.Reset(); + Mapper.Initialize(cfg => { }); + }); + action.ShouldNotThrow(); + } + } } \ No newline at end of file