11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Linq . Expressions ;
5+ using AutoMapper . Execution ;
36
47namespace AutoMapper
58{
69 /// <summary>
710 /// Context information regarding resolution of a destination value
811 /// </summary>
9- public class ResolutionContext
12+ public class ResolutionContext : IRuntimeMapper
1013 {
1114 private Dictionary < ContextCacheKey , object > _instanceCache ;
1215 private Dictionary < TypePair , int > _typeDepth ;
16+ private readonly IRuntimeMapper _inner ;
17+
18+ public ResolutionContext ( IMappingOperationOptions options , IRuntimeMapper mapper )
19+ {
20+ Options = options ;
21+ _inner = mapper ;
22+ }
1323
1424 /// <summary>
1525 /// Mapping operation options
1626 /// </summary>
1727 public IMappingOperationOptions Options { get ; }
1828
19- internal object GetDestination ( object source , Type destinationType )
20- {
21- InstanceCache . TryGetValue ( new ContextCacheKey ( source , destinationType ) , out object destination ) ;
22- return destination ;
23- }
29+ /// <summary>
30+ /// Context items from <see cref="Options"/>
31+ /// </summary>
32+ public IDictionary < string , object > Items => Options . Items ;
2433
25- internal void CacheDestination ( object source , Type destinationType , object destination )
26- {
27- InstanceCache [ new ContextCacheKey ( source , destinationType ) ] = destination ;
28- }
34+ /// <summary>
35+ /// Current mapper
36+ /// </summary>
37+ public IRuntimeMapper Mapper => this ;
38+
39+ public IConfigurationProvider ConfigurationProvider => _inner . ConfigurationProvider ;
40+
41+ Func < Type , object > IMapper . ServiceCtor => _inner . ServiceCtor ;
42+
43+ ResolutionContext IRuntimeMapper . DefaultContext => _inner . DefaultContext ;
2944
3045 /// <summary>
3146 /// Instance cache for resolving circular references
@@ -44,14 +59,6 @@ public Dictionary<ContextCacheKey, object> InstanceCache
4459 }
4560 }
4661
47- private void CheckDefault ( )
48- {
49- if ( IsDefault )
50- {
51- throw new InvalidOperationException ( ) ;
52- }
53- }
54-
5562 /// <summary>
5663 /// Instance cache for resolving keeping track of depth
5764 /// </summary>
@@ -69,6 +76,99 @@ private Dictionary<TypePair, int> TypeDepth
6976 }
7077 }
7178
79+ TDestination IMapper . Map < TDestination > ( object source )
80+ => ( TDestination ) _inner . Map ( source , null , source . GetType ( ) , typeof ( TDestination ) , this ) ;
81+
82+ TDestination IMapper . Map < TDestination > ( object source , Action < IMappingOperationOptions > opts )
83+ {
84+ opts ( Options ) ;
85+
86+ return ( ( IMapper ) this ) . Map < TDestination > ( source ) ;
87+ }
88+
89+ TDestination IMapper . Map < TSource , TDestination > ( TSource source )
90+ => _inner . Map ( source , default ( TDestination ) , this ) ;
91+
92+ TDestination IMapper . Map < TSource , TDestination > ( TSource source , Action < IMappingOperationOptions < TSource , TDestination > > opts )
93+ {
94+ var typedOptions = new MappingOperationOptions < TSource , TDestination > ( _inner . ServiceCtor ) ;
95+
96+ opts ( typedOptions ) ;
97+
98+ var destination = default ( TDestination ) ;
99+
100+ typedOptions . BeforeMapAction ( source , destination ) ;
101+
102+ destination = _inner . Map ( source , destination , this ) ;
103+
104+ typedOptions . AfterMapAction ( source , destination ) ;
105+
106+ return destination ;
107+ }
108+
109+ TDestination IMapper . Map < TSource , TDestination > ( TSource source , TDestination destination )
110+ => _inner . Map ( source , destination , this ) ;
111+
112+ TDestination IMapper . Map < TSource , TDestination > ( TSource source , TDestination destination , Action < IMappingOperationOptions < TSource , TDestination > > opts )
113+ {
114+ var typedOptions = new MappingOperationOptions < TSource , TDestination > ( _inner . ServiceCtor ) ;
115+
116+ opts ( typedOptions ) ;
117+
118+ typedOptions . BeforeMapAction ( source , destination ) ;
119+
120+ destination = _inner . Map ( source , destination , this ) ;
121+
122+ typedOptions . AfterMapAction ( source , destination ) ;
123+
124+ return destination ;
125+ }
126+
127+ object IMapper . Map ( object source , Type sourceType , Type destinationType )
128+ => _inner . Map ( source , null , sourceType , destinationType , this ) ;
129+
130+ object IMapper . Map ( object source , Type sourceType , Type destinationType , Action < IMappingOperationOptions > opts )
131+ {
132+ opts ( Options ) ;
133+
134+ return ( ( IMapper ) this ) . Map ( source , sourceType , destinationType ) ;
135+ }
136+
137+ object IMapper . Map ( object source , object destination , Type sourceType , Type destinationType )
138+ => _inner . Map ( source , destination , sourceType , destinationType , this ) ;
139+
140+ object IMapper . Map ( object source , object destination , Type sourceType , Type destinationType , Action < IMappingOperationOptions > opts )
141+ {
142+ opts ( Options ) ;
143+
144+ return ( ( IMapper ) this ) . Map ( source , destination , sourceType , destinationType ) ;
145+ }
146+
147+ object IRuntimeMapper . Map ( object source , object destination , Type sourceType , Type destinationType , ResolutionContext context ,
148+ IMemberMap memberMap )
149+ => _inner . Map ( source , destination , sourceType , destinationType , context , memberMap ) ;
150+
151+ TDestination IRuntimeMapper . Map < TSource , TDestination > ( TSource source , TDestination destination , ResolutionContext context ,
152+ IMemberMap memberMap )
153+ => _inner . Map ( source , destination , context , memberMap ) ;
154+
155+ IQueryable < TDestination > IMapper . ProjectTo < TDestination > ( IQueryable source , object parameters , params Expression < Func < TDestination , object > > [ ] membersToExpand )
156+ => _inner . ProjectTo ( source , parameters , membersToExpand ) ;
157+
158+ IQueryable < TDestination > IMapper . ProjectTo < TDestination > ( IQueryable source , IDictionary < string , object > parameters , params string [ ] membersToExpand )
159+ => _inner . ProjectTo < TDestination > ( source , parameters , membersToExpand ) ;
160+
161+ internal object GetDestination ( object source , Type destinationType )
162+ {
163+ InstanceCache . TryGetValue ( new ContextCacheKey ( source , destinationType ) , out object destination ) ;
164+ return destination ;
165+ }
166+
167+ internal void CacheDestination ( object source , Type destinationType , object destination )
168+ {
169+ InstanceCache [ new ContextCacheKey ( source , destinationType ) ] = destination ;
170+ }
171+
72172 internal void IncrementTypeDepth ( TypePair types )
73173 {
74174 TypeDepth [ types ] ++ ;
@@ -87,37 +187,24 @@ internal int GetTypeDepth(TypePair types)
87187 return TypeDepth [ types ] ;
88188 }
89189
90- /// <summary>
91- /// Current mapper
92- /// </summary>
93- public IRuntimeMapper Mapper { get ; }
94-
95- /// <summary>
96- /// Current configuration
97- /// </summary>
98- public IConfigurationProvider ConfigurationProvider => Mapper . ConfigurationProvider ;
99-
100- /// <summary>
101- /// Context items from <see cref="Options"/>
102- /// </summary>
103- public IDictionary < string , object > Items => Options . Items ;
104-
105- public ResolutionContext ( IMappingOperationOptions options , IRuntimeMapper mapper )
106- {
107- Options = options ;
108- Mapper = mapper ;
109- }
190+ internal void ValidateMap ( TypeMap typeMap )
191+ => ConfigurationProvider . AssertConfigurationIsValid ( typeMap ) ;
110192
111- internal bool IsDefault => this == Mapper . DefaultContext ;
193+ internal bool IsDefault => this == _inner . DefaultContext ;
112194
113195 internal TDestination Map < TSource , TDestination > ( TSource source , TDestination destination , IMemberMap memberMap )
114- => Mapper . Map ( source , destination , this , memberMap ) ;
196+ => _inner . Map ( source , destination , this , memberMap ) ;
115197
116- internal object Map ( object source , object destination , Type sourceType , Type destinationType , IMemberMap memberMap )
117- => Mapper . Map ( source , destination , sourceType , destinationType , this , memberMap ) ;
198+ internal object Map ( object source , object destination , Type sourceType , Type destinationType , IMemberMap memberMap )
199+ => _inner . Map ( source , destination , sourceType , destinationType , this , memberMap ) ;
118200
119- internal void ValidateMap ( TypeMap typeMap )
120- => ConfigurationProvider . AssertConfigurationIsValid ( typeMap ) ;
201+ private void CheckDefault ( )
202+ {
203+ if ( IsDefault )
204+ {
205+ throw new InvalidOperationException ( ) ;
206+ }
207+ }
121208 }
122209
123210 public struct ContextCacheKey : IEquatable < ContextCacheKey >
0 commit comments