Skip to content

Commit 34a94ee

Browse files
State
1 parent facb9c5 commit 34a94ee

File tree

3 files changed

+58
-65
lines changed

3 files changed

+58
-65
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.Experiments
4+
{
5+
public interface ICreateTask
6+
{
7+
}
8+
9+
public sealed class CreateTask<Info> : ICreateTask
10+
{
11+
public IEnumerable<ICreateTask> Subtasks { get; }
12+
13+
public CreateTask(IEnumerable<ICreateTask> subtasks)
14+
{
15+
Subtasks = subtasks;
16+
}
17+
}
18+
}

experiments/Azure.Experiments/Azure.Experiments/IState.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ namespace Microsoft.Azure.Experiments
55
public interface IState
66
{
77
ResourceGroup GetResourceGroup(string name);
8+
89
T Get<T>(ResourceConfig<T> resourceConfig)
910
where T : class;
10-
T Get<T>(IChildResourceConfig<T> childResourceConfig)
11-
where T : class;
11+
12+
T Get<T, P>(ChildResourceConfig<T, P> childResourceConfig)
13+
where T : class
14+
where P : class;
1215
}
1316
}

experiments/Azure.Experiments/Azure.Experiments/StateMap.cs renamed to experiments/Azure.Experiments/Azure.Experiments/State.cs

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,39 @@
99

1010
namespace 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

Comments
 (0)