@@ -25,7 +25,7 @@ public ReflectionModelReader(Options options, List<ITransferObject> transferObje
25
25
26
26
public ModelTransferObject Read ( Type type , IOptions caller = null )
27
27
{
28
- ModelTransferObject model = new ( ) { Language = ReflectionLanguage . Instance } ;
28
+ ModelTransferObject model = new ( ) { Language = ReflectionLanguage . Instance , Type = type } ;
29
29
model . Name = model . OriginalName = type . Name ;
30
30
model . Namespace = type . Namespace ;
31
31
model . IsNullable = ! type . IsValueType ;
@@ -34,17 +34,15 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
34
34
model . FromSystem = type . Namespace != null && type . Namespace . StartsWith ( "System" ) ;
35
35
36
36
IOptions typeOptions = this . options . Get ( type , caller ) ;
37
-
38
37
if ( model . IsGeneric )
39
38
{
40
39
model . Name = model . OriginalName = type . Name . Split ( '`' ) . First ( ) ;
41
40
model = new GenericModelTransferObject ( model ) ;
42
41
}
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 ) ) ;
46
43
if ( existingModel != null )
47
44
{
45
+ // TODO: Replace complete if with cached model reading after cloning of TransferModels is fixed
48
46
if ( model . IsGeneric )
49
47
{
50
48
GenericModelTransferObject genericModel = new ( existingModel ) ;
@@ -53,20 +51,14 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
53
51
this . ApplyGenericTemplate ( type , genericModel ) ;
54
52
this . transferObjects . Add ( existingModel ) ;
55
53
}
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
- }
68
54
return existingModel ;
69
55
}
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
+ // }
70
62
this . options . Set ( model , typeOptions ) ;
71
63
if ( typeOptions . Ignore )
72
64
{
@@ -102,6 +94,41 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
102
94
return model ;
103
95
}
104
96
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
+
105
132
private void ReadArray ( Type type , ModelTransferObject model )
106
133
{
107
134
// Logger.Trace($"Reflection read array {type.Name} ({type.Namespace})");
0 commit comments