9
9
10
10
namespace Microsoft . Azure . Experiments
11
11
{
12
- public sealed class StateMap : IState
12
+ public static class State
13
13
{
14
- public T Get < T > ( ResourceConfig < T > resourceConfig )
14
+ public static async Task < IState > GetStateAsync < T > ( this IClient client , ResourceConfig < T > config )
15
15
where T : class
16
- => Resources . TryGetValue ( resourceConfig , out var result ) ? ( T ) result : null ;
16
+ {
17
+ var visitor = new GetAsyncVisitor ( client ) ;
18
+ await visitor . Visit ( config ) ;
19
+ return visitor . State ;
20
+ }
17
21
18
- public T Get < T > ( IChildResourceConfig < T > childResourceConfig )
19
- where T : class
20
- => ChildResources . TryGetValue ( childResourceConfig , out var result ) ? ( T ) result : null ;
22
+ sealed class Result : IState
23
+ {
24
+ public T Get < T > ( ResourceConfig < T > resourceConfig )
25
+ where T : class
26
+ => Resources . TryGetValue ( resourceConfig , out var result ) ? ( T ) result : null ;
21
27
22
- public ResourceGroup GetResourceGroup ( string name )
23
- => ResourceGroups . TryGetValue ( name , out var result ) ? result : null ;
28
+ public T Get < T , P > ( ChildResourceConfig < T , P > config )
29
+ where T : class
30
+ where P : class
31
+ {
32
+ var parent = Get ( config . Parent ) ;
33
+ return parent == null ? null : config . Policy . Get ( parent , config . Name ) ;
34
+ }
24
35
25
- public Task GetAsync < T > ( IClient client , ResourceConfig < T > config )
26
- where T : class
27
- => new GetAsyncVisitor ( this , client ) . Visit ( config ) ;
36
+ public ResourceGroup GetResourceGroup ( string name )
37
+ => ResourceGroups . TryGetValue ( name , out var result ) ? result : null ;
38
+
39
+ public ConcurrentDictionary < string , ResourceGroup > ResourceGroups { get ; }
40
+ = new ConcurrentDictionary < string , ResourceGroup > ( ) ;
41
+
42
+ public ConcurrentDictionary < IResourceConfig , object > Resources { get ; }
43
+ = new ConcurrentDictionary < IResourceConfig , object > ( ) ;
44
+ }
28
45
29
46
static async Task < T > HandleNotFoundException < T > ( Func < Task < T > > f )
30
47
where T : class
@@ -44,9 +61,10 @@ sealed class GetAsyncVisitor :
44
61
IResourceConfigVisitor < Task > ,
45
62
IChildResourceConfigVisitor < Task >
46
63
{
47
- public GetAsyncVisitor ( StateMap map , IClient client )
64
+ public Result State { get ; } = new Result ( ) ;
65
+
66
+ public GetAsyncVisitor ( IClient client )
48
67
{
49
- State = map ;
50
68
Client = client ;
51
69
}
52
70
@@ -67,10 +85,9 @@ public async Task GetResourceGroupAsync(string name)
67
85
return result ;
68
86
} ) ;
69
87
70
- public async Task < Info > GetResourceAsync < Info > ( ResourceConfig < Info > config )
88
+ public async Task Visit < Info > ( ResourceConfig < Info > config )
71
89
where Info : class
72
- {
73
- var result = await ResourcesTasks . GetOrAdd (
90
+ => await ResourcesTasks . GetOrAdd (
74
91
config ,
75
92
async _ =>
76
93
{
@@ -94,44 +111,11 @@ public async Task<Info> GetResourceAsync<Info>(ResourceConfig<Info> config)
94
111
}
95
112
return i ;
96
113
} ) ;
97
- return result as Info ;
98
- }
99
-
100
- public async Task Visit < Info > ( ResourceConfig < Info > config )
101
- where Info : class
102
- => await GetResourceAsync ( config ) ;
103
114
104
- /// <summary>
105
- /// Get infromation about a child resource.
106
- /// </summary>
107
- /// <typeparam name="Info"></typeparam>
108
- /// <typeparam name="ParentInfo"></typeparam>
109
- /// <param name="config"></param>
110
- /// <returns></returns>
111
- public async Task Visit < Info , ParentInfo > ( ChildResourceConfig < Info , ParentInfo > config )
115
+ public Task Visit < Info , ParentInfo > ( ChildResourceConfig < Info , ParentInfo > config )
112
116
where Info : class
113
117
where ParentInfo : class
114
- => await ChildResourcesTasks . GetOrAdd (
115
- config ,
116
- async _ =>
117
- {
118
- var parent = await GetResourceAsync ( config . Parent ) ;
119
- if ( parent != null )
120
- {
121
- var result = config . Policy . Get ( parent , config . Name ) ;
122
- if ( result != null )
123
- {
124
- State . ChildResources . GetOrAdd ( config , result ) ;
125
- }
126
- return result ;
127
- }
128
- else
129
- {
130
- return null ;
131
- }
132
- } ) ;
133
-
134
- StateMap State { get ; }
118
+ => Visit ( config . Parent ) ;
135
119
136
120
IClient Client { get ; }
137
121
@@ -140,18 +124,6 @@ public async Task Visit<Info, ParentInfo>(ChildResourceConfig<Info, ParentInfo>
140
124
141
125
ConcurrentDictionary < IResourceConfig , Task < object > > ResourcesTasks { get ; }
142
126
= new ConcurrentDictionary < IResourceConfig , Task < object > > ( ) ;
143
-
144
- ConcurrentDictionary < IChildResourceConfig , Task < object > > ChildResourcesTasks { get ; }
145
- = new ConcurrentDictionary < IChildResourceConfig , Task < object > > ( ) ;
146
127
}
147
-
148
- ConcurrentDictionary < string , ResourceGroup > ResourceGroups { get ; }
149
- = new ConcurrentDictionary < string , ResourceGroup > ( ) ;
150
-
151
- ConcurrentDictionary < IResourceConfig , object > Resources { get ; }
152
- = new ConcurrentDictionary < IResourceConfig , object > ( ) ;
153
-
154
- ConcurrentDictionary < IChildResourceConfig , object > ChildResources { get ; }
155
- = new ConcurrentDictionary < IChildResourceConfig , object > ( ) ;
156
128
}
157
129
}
0 commit comments