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