1
- using System ;
1
+ using System . Linq ;
2
2
using System . Threading ;
3
3
using System . Threading . Tasks ;
4
4
5
5
namespace Microsoft . Azure . Commands . Common . Strategies
6
6
{
7
- /*
8
7
public static class CreateOrUpdateAsyncOperation
9
8
{
10
9
public static async Task < IState > CreateOrUpdateAsync < Model > (
@@ -14,219 +13,67 @@ public static async Task<IState> CreateOrUpdateAsync<Model>(
14
13
CancellationToken cancellationToken )
15
14
where Model : class
16
15
{
17
- var context = new Context(client, cancellationToken, target);
16
+ var context = new Context (
17
+ new AsyncOperationContext ( client , cancellationToken ) , target ) ;
18
+ await context . CreateOrUpdateAsync ( config ) ;
19
+ return context . OperationContext . Result ;
18
20
}
19
21
20
22
sealed class Context
21
23
{
22
- public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
23
-
24
- public IClient Client { get; }
24
+ public AsyncOperationContext OperationContext { get ; }
25
25
26
26
public IState Target { get ; }
27
27
28
- public CancellationToken CancellationToken { get; }
29
-
30
- public Context(IClient client, CancellationToken cancellationToken, IState target)
28
+ public Context ( AsyncOperationContext operationContext , IState target )
31
29
{
32
- Client = client;
33
- CancellationToken = cancellationToken;
30
+ OperationContext = operationContext ;
34
31
Target = target ;
35
32
}
36
- }
37
33
38
- sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
39
- {
40
- public async Task Visit<Model>(ResourceConfig<Model> config, Context context)
34
+ public async Task CreateOrUpdateAsync < Model > ( ResourceConfig < Model > config )
41
35
where Model : class
42
36
{
43
- var model = context. Target.Get(config);
37
+ var model = Target . Get ( config ) ;
44
38
if ( model != null )
45
39
{
46
- await context. OperationContext.GetOrAdd (
40
+ await OperationContext . GetOrAddAsync (
47
41
config ,
48
42
async ( ) =>
49
43
{
50
- foreach (var d in config.Dependencies)
51
- {
52
- }
44
+ var tasks = config . Dependencies . Select ( CreateOrUpdateAsync ) ;
45
+ await Task . WhenAll ( tasks ) ;
53
46
return await config . Strategy . CreateOrUpdateAsync (
54
- context .Client,
47
+ OperationContext . Client ,
55
48
CreateOrUpdateAsyncParams . Create (
56
49
config . ResourceGroupName ,
57
50
config . Name ,
58
51
model ,
59
- context .CancellationToken));
52
+ OperationContext . CancellationToken ) ) ;
60
53
} ) ;
61
54
}
62
55
}
63
56
64
- public Task Visit <Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context )
57
+ public Task CreateOrUpdateAsync < Model , ParentModel > ( NestedResourceConfig < Model , ParentModel > config )
65
58
where Model : class
66
59
where ParentModel : class
67
- {
68
- throw new NotImplementedException();
69
- }
70
- }
71
- }
72
-
73
- /*
74
- public static class CreateOrUpdateAsyncOperation
75
- {
76
- public static async Task<IState> CreateOrUpdateAsync<Model>(
77
- this ResourceConfig<Model> config,
78
- IClient client,
79
- IState target,
80
- CancellationToken cancellationToken)
81
- where Model : class
82
- {
83
- var context = new AsyncOperationContext();
84
- var model = target.Get(config);
85
- if (model != null)
86
- {
87
- await context.GetOrAdd(
88
- config,
89
- async () =>
90
- {
91
- // config.Dependencies
92
- return await config.Strategy.CreateOrUpdateAsync(
93
- client,
94
- CreateOrUpdateAsyncParams.Create(
95
- config.ResourceGroupName,
96
- config.Name,
97
- model,
98
- cancellationToken));
99
- });
100
- }
101
- return context.Result;
102
- }
103
-
104
- sealed class Context
105
- {
106
- AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
107
-
108
- IClient Client { get; }
109
-
110
- IState Target { get; }
111
-
112
- CancellationToken CancellationToken { get; }
113
-
114
- public async Task CreateOrUpdateAsync<Model>(ResourceConfig<Model> config)
115
- where Model : class
116
- {
117
- var model = Target.Get(config);
118
- if (model != null)
119
- {
120
- await OperationContext.GetOrAdd(
121
- config,
122
- async () =>
123
- {
124
- // config.Dependencies
125
- return await config.Strategy.CreateOrUpdateAsync(
126
- Client,
127
- CreateOrUpdateAsyncParams.Create(
128
- config.ResourceGroupName,
129
- config.Name,
130
- model,
131
- CancellationToken));
132
- });
133
- }
134
- }
60
+ => CreateOrUpdateAsync ( config . Parent ) ;
135
61
136
- public Context(IClient client, IState target, CancellationToken cancellationToken)
137
- {
138
- Client = client;
139
- Target = target;
140
- CancellationToken = cancellationToken;
141
- }
62
+ public Task CreateOrUpdateAsync ( IResourceBaseConfig config )
63
+ => config . Accept ( new Visitor ( ) , this ) ;
142
64
}
143
65
144
66
sealed class Visitor : IResourceBaseConfigVisitor < Context , Task >
145
67
{
146
68
public Task Visit < Model > ( ResourceConfig < Model > config , Context context )
147
69
where Model : class
148
- {
149
- throw new NotImplementedException();
150
- }
70
+ => context . CreateOrUpdateAsync ( config ) ;
151
71
152
72
public Task Visit < Model , ParentModel > (
153
73
NestedResourceConfig < Model , ParentModel > config , Context context )
154
74
where Model : class
155
75
where ParentModel : class
156
- {
157
- throw new NotImplementedException();
158
- }
159
- }
160
- }
161
-
162
- /*
163
- public static class CreateOrUpdateAsyncOperation
164
- {
165
- /// <summary>
166
- /// Asynchronous resource creation.
167
- /// </summary>
168
- /// <typeparam name="Model"></typeparam>
169
- /// <param name="config"></param>
170
- /// <param name="client"></param>
171
- /// <param name="current"></param>
172
- /// <param name="target"></param>
173
- /// <param name="cancellationToken"></param>
174
- /// <returns>An Azure state.</returns>
175
- public static async Task<IState> CreateOrUpdateAsync<Model>(
176
- this IResourceBaseConfig<Model> config,
177
- IClient client,
178
- IState target,
179
- CancellationToken cancellationToken)
180
- where Model : class
181
- {
182
- var visitor = new CreateAsyncVisitor(client, target, cancellationToken);
183
- await visitor.GetOrAdd(config);
184
- return visitor.Result;
185
- }
186
-
187
- sealed class CreateAsyncVisitor : AsyncOperationVisitor
188
- {
189
- public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
190
- {
191
- var target = Target.Get(config);
192
- if (target == null)
193
- {
194
- return null;
195
- }
196
- var tasks = config.Dependencies.Select(GetOrAddUntyped);
197
- await Task.WhenAll(tasks);
198
- return await config.Strategy.CreateOrUpdateAsync(
199
- Client,
200
- CreateOrUpdateAsyncParams.Create(
201
- config.ResourceGroupName,
202
- config.Name,
203
- Target.Get(config),
204
- CancellationToken));
205
- }
206
-
207
- public override async Task<object> Visit<Model, ParentModel>(
208
- NestedResourceConfig<Model, ParentModel> config)
209
- {
210
- var target = Target.GetNestedResourceModel(config);
211
- if (target == null)
212
- {
213
- return null;
214
- }
215
- var parent = await GetOrAdd(config.Parent);
216
- return config.Strategy.Get(parent, config.Name);
217
- }
218
-
219
- public CreateAsyncVisitor(
220
- IClient client,
221
- IState target,
222
- CancellationToken cancellationToken)
223
- : base(client, cancellationToken)
224
- {
225
- Target = target;
226
- }
227
-
228
- IState Target { get; }
76
+ => context . CreateOrUpdateAsync ( config ) ;
229
77
}
230
78
}
231
- */
232
79
}
0 commit comments