99
1010namespace Microsoft . Azure . Experiments
1111{
12- public sealed class StateMap : IState
12+ public static class State
1313 {
14- public T Get < T > ( ResourceConfig < T > resourceConfig )
14+ public static async Task < IState > GetStateAsync < T > ( this IClient client , ResourceConfig < T > config )
1515 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+ }
1721
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 ;
2127
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+ }
2435
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+ }
2845
2946 static async Task < T > HandleNotFoundException < T > ( Func < Task < T > > f )
3047 where T : class
@@ -44,9 +61,10 @@ sealed class GetAsyncVisitor :
4461 IResourceConfigVisitor < Task > ,
4562 IChildResourceConfigVisitor < Task >
4663 {
47- public GetAsyncVisitor ( StateMap map , IClient client )
64+ public Result State { get ; } = new Result ( ) ;
65+
66+ public GetAsyncVisitor ( IClient client )
4867 {
49- State = map ;
5068 Client = client ;
5169 }
5270
@@ -67,10 +85,9 @@ public async Task GetResourceGroupAsync(string name)
6785 return result ;
6886 } ) ;
6987
70- public async Task < Info > GetResourceAsync < Info > ( ResourceConfig < Info > config )
88+ public async Task Visit < Info > ( ResourceConfig < Info > config )
7189 where Info : class
72- {
73- var result = await ResourcesTasks . GetOrAdd (
90+ => await ResourcesTasks . GetOrAdd (
7491 config ,
7592 async _ =>
7693 {
@@ -94,44 +111,11 @@ public async Task<Info> GetResourceAsync<Info>(ResourceConfig<Info> config)
94111 }
95112 return i ;
96113 } ) ;
97- return result as Info ;
98- }
99-
100- public async Task Visit < Info > ( ResourceConfig < Info > config )
101- where Info : class
102- => await GetResourceAsync ( config ) ;
103114
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 )
112116 where Info : class
113117 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 ) ;
135119
136120 IClient Client { get ; }
137121
@@ -140,18 +124,6 @@ public async Task Visit<Info, ParentInfo>(ChildResourceConfig<Info, ParentInfo>
140124
141125 ConcurrentDictionary < IResourceConfig , Task < object > > ResourcesTasks { get ; }
142126 = new ConcurrentDictionary < IResourceConfig , Task < object > > ( ) ;
143-
144- ConcurrentDictionary < IChildResourceConfig , Task < object > > ChildResourcesTasks { get ; }
145- = new ConcurrentDictionary < IChildResourceConfig , Task < object > > ( ) ;
146127 }
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 > ( ) ;
156128 }
157129}
0 commit comments