@@ -25,7 +25,7 @@ public ReflectionModelReader(Options options, List<ITransferObject> transferObje
2525
2626 public ModelTransferObject Read ( Type type , IOptions caller = null )
2727 {
28- ModelTransferObject model = new ( ) { Language = ReflectionLanguage . Instance } ;
28+ ModelTransferObject model = new ( ) { Language = ReflectionLanguage . Instance , Type = type } ;
2929 model . Name = model . OriginalName = type . Name ;
3030 model . Namespace = type . Namespace ;
3131 model . IsNullable = ! type . IsValueType ;
@@ -34,17 +34,15 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
3434 model . FromSystem = type . Namespace != null && type . Namespace . StartsWith ( "System" ) ;
3535
3636 IOptions typeOptions = this . options . Get ( type , caller ) ;
37-
3837 if ( model . IsGeneric )
3938 {
4039 model . Name = model . OriginalName = type . Name . Split ( '`' ) . First ( ) ;
4140 model = new GenericModelTransferObject ( model ) ;
4241 }
43- ModelTransferObject existingModel = this . transferObjects . Concat ( this . environment . TransferObjects )
44- . OfType < ModelTransferObject > ( )
45- . FirstOrDefault ( entry => entry . Equals ( model ) ) ;
42+ ModelTransferObject existingModel = this . transferObjects . OfType < ModelTransferObject > ( ) . FirstOrDefault ( entry => entry . Equals ( model ) ) ;
4643 if ( existingModel != null )
4744 {
45+ // TODO: Replace complete if with cached model reading after cloning of TransferModels is fixed
4846 if ( model . IsGeneric )
4947 {
5048 GenericModelTransferObject genericModel = new ( existingModel ) ;
@@ -53,20 +51,14 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
5351 this . ApplyGenericTemplate ( type , genericModel ) ;
5452 this . transferObjects . Add ( existingModel ) ;
5553 }
56- else
57- {
58- if ( ! this . transferObjects . Contains ( existingModel ) )
59- {
60- existingModel = existingModel . Clone ( ) ;
61- this . transferObjects . Add ( existingModel ) ;
62- }
63- if ( ! this . options . Contains ( existingModel ) )
64- {
65- this . options . Set ( existingModel , typeOptions ) ;
66- }
67- }
6854 return existingModel ;
6955 }
56+ // TODO: Uncomment cached model reading after cloning of TransferModels is fixed
57+ // existingModel = this.environment.TransferObjects.OfType<ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));
58+ // if (existingModel != null)
59+ // {
60+ // return this.ReadExisting(existingModel, caller);
61+ // }
7062 this . options . Set ( model , typeOptions ) ;
7163 if ( typeOptions . Ignore )
7264 {
@@ -102,6 +94,41 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
10294 return model ;
10395 }
10496
97+ private ModelTransferObject ReadExisting ( ModelTransferObject model , IOptions caller )
98+ {
99+ IOptions typeOptions = this . options . Get ( model . Type , caller ) ;
100+ if ( model . IsGeneric )
101+ {
102+ GenericModelTransferObject genericModel = new ( model ) ;
103+ model = genericModel ;
104+ if ( model . Type != null )
105+ {
106+ this . ApplyGenericTemplate ( model . Type , genericModel ) ;
107+ }
108+ }
109+ else
110+ {
111+ model = model . Clone ( ) ;
112+ }
113+ if ( ! this . options . Contains ( model ) )
114+ {
115+ this . options . Set ( model , typeOptions ) ;
116+ }
117+ this . transferObjects . Add ( model ) ;
118+ this . ReadExistingMembers ( model , typeOptions ) ;
119+ return model ;
120+ }
121+
122+ private void ReadExistingMembers ( ModelTransferObject model , IOptions caller )
123+ {
124+ model . Interfaces . OfType < ModelTransferObject > ( )
125+ . Concat ( model . Generics . Select ( x => x . Type ) )
126+ . Concat ( model . Constants . Select ( x => x . Type ) )
127+ . Concat ( model . Fields . Select ( x => x . Type ) )
128+ . Concat ( model . Properties . Select ( x => x . Type ) )
129+ . OfType < ModelTransferObject > ( ) . ForEach ( x => this . ReadExisting ( x , caller ) ) ;
130+ }
131+
105132 private void ReadArray ( Type type , ModelTransferObject model )
106133 {
107134 // Logger.Trace($"Reflection read array {type.Name} ({type.Namespace})");
0 commit comments