@@ -10,16 +10,12 @@ namespace JSONAPI.Core
1010{
1111 public class ModelManager : IModelManager
1212 {
13- public ModelManager ( ) {
14- _pluralizationService = new PluralizationService ( ) ;
15- }
16-
1713 public ModelManager ( IPluralizationService pluralizationService )
1814 {
1915 _pluralizationService = pluralizationService ;
2016 }
2117
22- private IPluralizationService _pluralizationService = null ;
18+ protected IPluralizationService _pluralizationService = null ;
2319 public IPluralizationService PluralizationService
2420 {
2521 get
@@ -30,21 +26,31 @@ public IPluralizationService PluralizationService
3026
3127 #region Cache storage
3228
33- private Lazy < Dictionary < Type , PropertyInfo > > _idProperties
29+ protected Lazy < Dictionary < Type , PropertyInfo > > _idProperties
3430 = new Lazy < Dictionary < Type , PropertyInfo > > (
3531 ( ) => new Dictionary < Type , PropertyInfo > ( )
3632 ) ;
3733
38- private Lazy < Dictionary < Type , Dictionary < string , PropertyInfo > > > _propertyMaps
34+ protected Lazy < Dictionary < Type , Dictionary < string , PropertyInfo > > > _propertyMaps
3935 = new Lazy < Dictionary < Type , Dictionary < string , PropertyInfo > > > (
4036 ( ) => new Dictionary < Type , Dictionary < string , PropertyInfo > > ( )
4137 ) ;
4238
43- private Lazy < Dictionary < Type , string > > _jsonKeysForType
39+ protected Lazy < Dictionary < Type , string > > _jsonKeysForType
4440 = new Lazy < Dictionary < Type , string > > (
4541 ( ) => new Dictionary < Type , string > ( )
4642 ) ;
4743
44+ protected Lazy < Dictionary < Type , bool > > _isSerializedAsMany
45+ = new Lazy < Dictionary < Type , bool > > (
46+ ( ) => new Dictionary < Type , bool > ( )
47+ ) ;
48+
49+ protected Lazy < Dictionary < Type , Type > > _getElementType
50+ = new Lazy < Dictionary < Type , Type > > (
51+ ( ) => new Dictionary < Type , Type > ( )
52+ ) ;
53+
4854 #endregion
4955
5056 #region Id property determination
@@ -76,7 +82,7 @@ public PropertyInfo GetIdProperty(Type type)
7682
7783 #region Property Maps
7884
79- protected IDictionary < string , PropertyInfo > GetPropertyMap ( Type type ) //FIXME: Will become protected
85+ protected IDictionary < string , PropertyInfo > GetPropertyMap ( Type type )
8086 {
8187 Dictionary < string , PropertyInfo > propMap = null ;
8288
@@ -158,20 +164,41 @@ protected static string FormatPropertyName(string propertyName)
158164
159165 public bool IsSerializedAsMany ( Type type )
160166 {
161- bool isMany =
162- type . IsArray ||
163- ( type . GetInterfaces ( ) . Contains ( typeof ( System . Collections . IEnumerable ) ) && type . IsGenericType ) ;
167+ bool isMany ;
168+
169+ var isManyCache = _isSerializedAsMany . Value ;
170+
171+ lock ( isManyCache )
172+ {
173+ if ( isManyCache . TryGetValue ( type , out isMany ) ) return isMany ;
174+
175+ isMany =
176+ type . IsArray ||
177+ ( type . GetInterfaces ( ) . Contains ( typeof ( System . Collections . IEnumerable ) ) && type . IsGenericType ) ;
178+
179+ isManyCache . Add ( type , isMany ) ;
180+ }
164181
165182 return isMany ;
166183 }
167184
168185 public Type GetElementType ( Type manyType )
169186 {
170187 Type etype = null ;
171- if ( manyType . IsGenericType )
172- etype = manyType . GetGenericArguments ( ) [ 0 ] ;
173- else
174- etype = manyType . GetElementType ( ) ;
188+
189+ var etypeCache = _getElementType . Value ;
190+
191+ lock ( etypeCache )
192+ {
193+ if ( etypeCache . TryGetValue ( manyType , out etype ) ) return etype ;
194+
195+ if ( manyType . IsGenericType )
196+ etype = manyType . GetGenericArguments ( ) [ 0 ] ;
197+ else
198+ etype = manyType . GetElementType ( ) ;
199+
200+ etypeCache . Add ( manyType , etype ) ;
201+ }
175202
176203 return etype ;
177204 }
0 commit comments