Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 9 additions & 0 deletions src/AutoMapper/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public static void Initialize(MapperConfigurationExpression config)
Instance = new Mapper(Configuration);
}

/// <summary>
/// Resets the mapper configuration. Not intended for production use, but for testing scenarios.
/// </summary>
public static void Reset()
{
_configuration = null;
_instance = null;
}

/// <summary>
/// Execute a mapping from the source object to a new destination object.
/// The source type is inferred from the source object.
Expand Down
47 changes: 39 additions & 8 deletions src/UnitTests/StaticMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using QueryableExtensions;
using System;

#if !NET40
[Collection(nameof(StaticMapping))]
#endif
public class StaticMapping
{
public class ModelObject
Expand Down Expand Up @@ -62,20 +65,26 @@ public class Dest
public int Value { get; set; }
}

static StaticMapping()
private void InitializeMapping()
{
Mapper.Initialize(cfg =>
lock (this)
{
cfg.CreateMap<Source, Dest>();
cfg.CreateMap<ModelObject, ModelDto>();
cfg.CreateMap<ModelObject2, ModelDto2>();
cfg.CreateMap<ModelObject3, ModelDto3>(MemberList.Source);
});
Mapper.Reset();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source, Dest>();
cfg.CreateMap<ModelObject, ModelDto>();
cfg.CreateMap<ModelObject2, ModelDto2>();
cfg.CreateMap<ModelObject3, ModelDto3>(MemberList.Source);
});
}
}

[Fact]
public void Can_map_statically()
{
InitializeMapping();

var source = new Source {Value = 5};

var dest = Mapper.Map<Source, Dest>(source);
Expand All @@ -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();

Expand All @@ -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();
}

}
}