@@ -41,7 +41,7 @@ public class MapperConfiguration : IGlobalConfiguration
4141 private readonly Dictionary < TypePair , TypeMap > _configuredMaps ;
4242 private readonly Dictionary < TypePair , TypeMap > _resolvedMaps ;
4343 private readonly LockingConcurrentDictionary < TypePair , TypeMap > _runtimeMaps ;
44- private readonly ProjectionBuilder _projectionBuilder ;
44+ private ProjectionBuilder _projectionBuilder ;
4545 private readonly LockingConcurrentDictionary < MapRequest , Delegate > _executionPlans ;
4646 private readonly ConfigurationValidator _validator ;
4747 private readonly Features < IRuntimeFeature > _features = new ( ) ;
@@ -71,13 +71,10 @@ public MapperConfiguration(MapperConfigurationExpression configurationExpression
7171 _mappers = configuration . Mappers . ToArray ( ) ;
7272 _executionPlans = new ( CompileExecutionPlan ) ;
7373 _validator = new ( configuration ) ;
74- _projectionBuilder = new ( this , configuration . ProjectionMappers . ToArray ( ) ) ;
75-
7674 _serviceCtor = configuration . ServiceCtor ;
7775 _enableNullPropagationForQueryMapping = configuration . EnableNullPropagationForQueryMapping ?? false ;
7876 _maxExecutionPlanDepth = configuration . MaxExecutionPlanDepth + 1 ;
7977 _recursiveQueriesMaxDepth = configuration . RecursiveQueriesMaxDepth ;
80-
8178 Configuration = new ( ( IProfileConfiguration ) configuration ) ;
8279 int typeMapsCount = Configuration . TypeMapsCount ;
8380 int openTypeMapsCount = Configuration . OpenTypeMapsCount ;
@@ -235,7 +232,18 @@ LambdaExpression GenerateObjectMapperExpression(in MapRequest mapRequest, IObjec
235232 return Lambda ( fullExpression , source , destination , ContextParameter ) ;
236233 }
237234 }
238- IProjectionBuilder IGlobalConfiguration . ProjectionBuilder => _projectionBuilder ;
235+ IProjectionBuilder IGlobalConfiguration . ProjectionBuilder
236+ {
237+ get
238+ {
239+ if ( _projectionBuilder == null )
240+ {
241+ CreateProjectionBuilder ( ) ;
242+ }
243+ return _projectionBuilder ;
244+ void CreateProjectionBuilder ( ) => Interlocked . CompareExchange ( ref _projectionBuilder , new ( this , _validator . Expression . ProjectionMappers . ToArray ( ) ) , null ) ;
245+ }
246+ }
239247 Func < Type , object > IGlobalConfiguration . ServiceCtor => _serviceCtor ;
240248 bool IGlobalConfiguration . EnableNullPropagationForQueryMapping => _enableNullPropagationForQueryMapping ;
241249 int IGlobalConfiguration . MaxExecutionPlanDepth => _maxExecutionPlanDepth ;
0 commit comments