Skip to content

Commit

Permalink
new interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 17, 2017
1 parent 803c3c0 commit 88a5696
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override async Task<object> Visit<Model, ParentModel>(
return null;
}
var parent = await GetOrAdd(config.Parent);
return config.Policy.Get(parent, config.Name);
return config.Strategy.Get(parent, config.Name);
}

public CreateAsyncVisitor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var parent = await GetOrAdd(config.Parent);
return parent == null ? null : config.Policy.Get(parent, config.Name);
return parent == null ? null : config.Strategy.Get(parent, config.Name);
}

public Visitor(IClient client, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Microsoft.Azure.Commands.Common.Strategies
{
public interface IResourceBaseConfig
{
IResourceBaseStrategy Strategy { get; }

string Name { get; }

Result Apply<Result>(IResourceBaseConfigVisitor<Result> visitor);

IEnumerable<string> GetId(string subscription);
Expand Down
23 changes: 6 additions & 17 deletions src/ResourceManager/Common/Commands.Common.Strategies/IState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,33 @@ Model GetOrNull<Model>(IResourceBaseConfig<Model> config)
object GetOrNullUntyped(IResourceBaseConfig config);
}

/*
public static class StateExtension
{
public static Model GetNestedOrNull<Model, ParentModel>(
this IState state, NestedResourceConfig<Model, ParentModel> config)
where Model : class
where ParentModel : class
{
var parentModel = config.Parent.Apply(new GetOrNullVisitor(state)) as Model;
var parentModel = config.Parent.Apply(new GetOrNullVisitor<ParentModel>(state));
return config.Strategy.Get(parentModel, config.Name);
}

sealed class GetOrNullVisitor : IResourceConfigVisitor<object>
sealed class GetOrNullVisitor<Model> : IResourceBaseConfigVisitor<Model, Model>
where Model : class
{
public GetOrNullVisitor(IState state)
{
State = state;
}

public object Visit<Model>(ResourceConfig<Model> config) where Model : class
public Model Visit(ResourceConfig<Model> config)
=> State.GetOrNull(config);

public object Visit<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config)
where Model : class
public Model Visit<ParentModel>(NestedResourceConfig<Model, ParentModel> config)
where ParentModel : class
=> State.GetNestedOrNull(config);

IState State { get; }
}
}
*/

/*
public interface IState
{
Model GetOrNull<Model>(IResourceConfig<Model> config)
where Model : class;
object GetOrNullUntyped(IResourceConfig config);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ public sealed class NestedResourceConfig<Model, ParentModel> : IResourceBaseConf
where Model : class
where ParentModel : class
{
public NestedResourceStrategy<Model, ParentModel> Policy { get; }
public NestedResourceStrategy<Model, ParentModel> Strategy { get; }

public string Name { get; }

public IResourceBaseConfig<ParentModel> Parent { get; }

public Func<Model> CreateModel { get; }

IResourceBaseStrategy IResourceBaseConfig.Strategy => Strategy;

public NestedResourceConfig(
NestedResourceStrategy<Model, ParentModel> policy,
NestedResourceStrategy<Model, ParentModel> strategy,
IResourceBaseConfig<ParentModel> parent,
string name,
Func<Model> createModel)
{
Policy = policy;
Strategy = strategy;
Name = name;
Parent = parent;
CreateModel = createModel;
Expand All @@ -47,6 +49,6 @@ public Result Apply<Result>(IResourceBaseConfigVisitor<Model, Result> visitor)
=> visitor.Visit(this);

public IEnumerable<string> GetId(string subscription)
=> Parent.GetId(subscription).Concat(Policy.GetId(Name));
=> Parent.GetId(subscription).Concat(Strategy.GetId(Name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

namespace Microsoft.Azure.Commands.Common.Strategies
{
public sealed class ResourceConfig<Model> : IResourceBaseConfig<Model>
public interface IResourceConfig : IResourceBaseConfig
{
string ResourceGroupName { get; }

IEnumerable<IResourceBaseConfig> Dependencies { get; }
}

public sealed class ResourceConfig<Model> : IResourceBaseConfig<Model>, IResourceConfig
where Model : class
{
public ResourceStrategy<Model> Strategy { get; }
Expand All @@ -18,6 +25,8 @@ public sealed class ResourceConfig<Model> : IResourceBaseConfig<Model>

public IEnumerable<IResourceBaseConfig> Dependencies { get; }

IResourceBaseStrategy IResourceBaseConfig.Strategy => Strategy;

public ResourceConfig(
ResourceStrategy<Model> strategy,
string resourceGroupName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public object Visit<Model, ParentModel>(
where ParentModel : class
{
var model = config.CreateModel();
config.Policy.Set(Get(config.Parent), config.Name, model);
config.Strategy.Set(Get(config.Parent), config.Name, model);
return model;
}

Expand Down

0 comments on commit 88a5696

Please sign in to comment.