Skip to content

Commit

Permalink
move initialize using query parameters convention to rich transient f…
Browse files Browse the repository at this point in the history
…eature
  • Loading branch information
dncsvr committed Oct 24, 2024
1 parent 427226e commit bf48f63
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void Configure(LayerConfigurator configurator)

configurator.ConfigureApiModelConventions(conventions =>
{
conventions.Add(new InitializeUsingQueryParametersConvention(), order: -10);
conventions.Add(new IncludeClassDocsForActionNamesConvention(_methodNames), order: -10);
conventions.Add(new UseClassNameInsteadOfActionNamesConvention(_methodNames), order: -10);
conventions.Add(new RemoveFromRouteConvention(_methodNames));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Baked.CodingStyle.RichTransient;

public class TargetRichTransientFromRouteConvention(DomainModel _domain)
public class InitializeUsingIdParameterConvention(DomainModel _domain)
: IApiModelConvention<ActionModelContext>
{
public void Apply(ActionModelContext context)
Expand All @@ -30,9 +30,12 @@ public void Apply(ActionModelContext context)
RoutePosition = 1
};

var targetParameter = context.Action.Parameter["target"];
targetParameter.Name = "newTarget";
targetParameter.Type = $"Func<{targetParameter.Type}>";

context.Action.RouteParts.RemoveAt(0);
context.Action.RouteParts.Insert(0, context.Controller.MappedType.Name.Pluralize());

context.Action.FindTargetStatement = $"newTarget().{initializer.Name}({initializer.DefaultOverload.Parameters.Select(p => $"@{p.Name}").Join(", ")})";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using Baked.RestApi.Configuration;
using Baked.RestApi.Model;

namespace Baked.CodingStyle.CommandPattern;
namespace Baked.CodingStyle.RichTransient;

public class InitializeUsingQueryParametersConvention : IApiModelConvention<ActionModelContext>
{
public void Apply(ActionModelContext context)
{
if (!context.Controller.MappedType.TryGetMembers(out var members)) { return; }
if (!members.Has<PubliclyInitializableAttribute>()) { return; }
if (members.Has<LocatableAttribute>()) { return; }
if (!members.Methods.Having<InitializerAttribute>().Any()) { return; }

var initializer = members.Methods.Having<InitializerAttribute>().Single();
foreach (var parameter in initializer.DefaultOverload.Parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void Configure(LayerConfigurator configurator)
{
var domainModel = configurator.Context.GetDomainModel();
conventions.Add(new TargetRichTransientFromRouteConvention(domainModel));
conventions.Add(new InitializeUsingQueryParametersConvention(), order: -10);
conventions.Add(new InitializeUsingIdParameterConvention(domainModel));
conventions.Add(new RichTransientInitializerIsGetResourceConvention(domainModel));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void Apply(ActionModelContext context)
parameter.From = RestApi.Model.ParameterModelFrom.Route;
parameter.RoutePosition = 1;

var targetParameter = context.Action.Parameter["target"];
targetParameter.Name = "newTarget";
targetParameter.Type = $"Func<{targetParameter.Type}>";

context.Action.Method = HttpMethod.Get;
context.Action.RouteParts = [context.Controller.MappedType.Name.Pluralize()];
context.Action.FindTargetStatement = "newTarget()";
Expand Down

0 comments on commit bf48f63

Please sign in to comment.