1- using  System ; 
1+ using  System . Linq ; 
22using  System . Threading ; 
33using  System . Threading . Tasks ; 
44
55namespace  Microsoft . Azure . Commands . Common . Strategies 
66{ 
7-     /* 
87    public  static class  CreateOrUpdateAsyncOperation 
98    { 
109        public  static async  Task < IState >  CreateOrUpdateAsync < Model > ( 
@@ -14,219 +13,67 @@ public static async Task<IState> CreateOrUpdateAsync<Model>(
1413            CancellationToken  cancellationToken ) 
1514            where  Model  :  class 
1615        { 
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 ; 
1820        } 
1921
2022        sealed  class  Context 
2123        { 
22-             public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext(); 
23- 
24-             public IClient Client { get; } 
24+             public  AsyncOperationContext  OperationContext  {  get ;  } 
2525
2626            public  IState  Target  {  get ;  } 
2727
28-             public CancellationToken CancellationToken { get; } 
29- 
30-             public Context(IClient client, CancellationToken cancellationToken, IState target) 
28+             public  Context ( AsyncOperationContext  operationContext ,  IState  target ) 
3129            { 
32-                 Client = client; 
33-                 CancellationToken = cancellationToken; 
30+                 OperationContext  =  operationContext ; 
3431                Target  =  target ; 
3532            } 
36-         } 
3733
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 ) 
4135                where  Model  :  class 
4236            { 
43-                 var model = context. Target.Get(config); 
37+                 var  model  =  Target . Get ( config ) ; 
4438                if  ( model  !=  null ) 
4539                { 
46-                     await context. OperationContext.GetOrAdd ( 
40+                     await  OperationContext . GetOrAddAsync ( 
4741                        config , 
4842                        async  ( )  => 
4943                        { 
50-                             foreach (var d in config.Dependencies) 
51-                             { 
52-                             }                             
44+                             var  tasks  =  config . Dependencies . Select ( CreateOrUpdateAsync ) ; 
45+                             await  Task . WhenAll ( tasks ) ; 
5346                            return  await  config . Strategy . CreateOrUpdateAsync ( 
54-                                 context .Client, 
47+                                 OperationContext . Client , 
5548                                CreateOrUpdateAsyncParams . Create ( 
5649                                    config . ResourceGroupName , 
5750                                    config . Name , 
5851                                    model , 
59-                                     context .CancellationToken)); 
52+                                     OperationContext . CancellationToken ) ) ; 
6053                        } ) ; 
6154                } 
6255            } 
6356
64-             public Task Visit <Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context ) 
57+             public  Task  CreateOrUpdateAsync < Model ,  ParentModel > ( NestedResourceConfig < Model ,  ParentModel >  config ) 
6558                where  Model  :  class 
6659                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 ) ; 
13561
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 ) ; 
14264        } 
14365
14466        sealed  class  Visitor  :  IResourceBaseConfigVisitor < Context ,  Task > 
14567        { 
14668            public  Task  Visit < Model > ( ResourceConfig < Model >  config ,  Context  context ) 
14769                where  Model  :  class 
148-             { 
149-                 throw new NotImplementedException(); 
150-             } 
70+                 =>  context . CreateOrUpdateAsync ( config ) ; 
15171
15272            public  Task  Visit < Model ,  ParentModel > ( 
15373                NestedResourceConfig < Model ,  ParentModel >  config ,  Context  context ) 
15474                where  Model  :  class 
15575                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 ) ; 
22977        } 
23078    } 
231-     */ 
23279} 
0 commit comments