-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add using rich transients as parameters test case
- add lookup transients by id conventions
- Loading branch information
Showing
6 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...ked.Recipe.Service.Application/CodingStyle/RichTransient/LookUpTransientByIdConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Baked.Business; | ||
using Baked.Domain.Model; | ||
using Baked.RestApi.Configuration; | ||
|
||
namespace Baked.CodingStyle.RichTransient; | ||
|
||
public class LookUpTransientByIdConvention(DomainModel _domain) | ||
: IApiModelConvention<ParameterModelContext> | ||
{ | ||
public void Apply(ParameterModelContext context) | ||
{ | ||
if (context.Action.MappedMethod is null) { return; } | ||
if (context.Action.MappedMethod.Has<InitializerAttribute>()) { return; } | ||
if (!context.Parameter.IsInvokeMethodParameter) { return; } | ||
if (context.Parameter.MappedParameter is null) { return; } | ||
if (!context.Parameter.MappedParameter.ParameterType.TryGetMembers(out var members)) { return; } | ||
if (!members.Has<LocatableAttribute>()) { return; } | ||
if (members.TryGetQueryType(_domain, out var _)) { return; } | ||
|
||
var initializer = members.Methods.Having<InitializerAttribute>().Single(); | ||
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var parameter)) { return; } | ||
|
||
var factoryParameter = context.Action.AddFactoryAsService(_domain, context.Parameter.MappedParameter.ParameterType); | ||
context.Parameter.Name = $"{context.Parameter.Name}Id"; | ||
context.Parameter.Type = "string"; | ||
context.Parameter.LookupRenderer = p => factoryParameter.BuildInitializer(p); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...d.Recipe.Service.Application/CodingStyle/RichTransient/LookUpTransientsByIdsConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Baked.Business; | ||
using Baked.Domain.Model; | ||
using Baked.RestApi.Configuration; | ||
using Humanizer; | ||
|
||
namespace Baked.CodingStyle.RichTransient; | ||
|
||
public class LookUpTransientsByIdsConvention(DomainModel _domain) | ||
: IApiModelConvention<ParameterModelContext> | ||
{ | ||
public void Apply(ParameterModelContext context) | ||
{ | ||
if (context.Action.MappedMethod is null) { return; } | ||
if (context.Action.MappedMethod.Has<InitializerAttribute>()) { return; } | ||
if (!context.Parameter.IsInvokeMethodParameter) { return; } | ||
if (context.Parameter.MappedParameter is null) { return; } | ||
if (!context.Parameter.MappedParameter.ParameterType.TryGetElementType(out var elementType)) { return; } | ||
if (elementType.TryGetQueryContextType(_domain, out var queryContextType)) { return; } | ||
if (!elementType.GetMetadata().Has<LocatableAttribute>()) { return; } | ||
|
||
var initializer = elementType.GetMembers().Methods.Having<InitializerAttribute>().Single(); | ||
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var parameter)) { return; } | ||
|
||
var factoryParameter = context.Action.AddFactoryAsService(_domain, elementType); | ||
|
||
context.Parameter.Type = "IEnumerable<string>"; | ||
context.Parameter.Name = $"{context.Parameter.Name.Singularize()}Ids"; | ||
context.Parameter.LookupRenderer = p => factoryParameter.BuildInitializerByIds(p, isArray: context.Parameter.TypeModel.IsArray); | ||
} | ||
} |
44 changes: 43 additions & 1 deletion
44
...ecipe.Service.Application/CodingStyle/RichTransient/RichTransientCodingStyleExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,52 @@ | ||
using Baked.CodingStyle; | ||
using Baked.Business; | ||
using Baked.CodingStyle; | ||
using Baked.CodingStyle.RichTransient; | ||
using Baked.Domain.Model; | ||
using Baked.RestApi.Model; | ||
using Humanizer; | ||
using ParameterModel = Baked.RestApi.Model.ParameterModel; | ||
|
||
namespace Baked; | ||
|
||
public static class RichTransientCodingStyleExtensions | ||
{ | ||
public static RichTransientCodingStyleFeature RichTransient(this CodingStyleConfigurator _) => | ||
new(); | ||
|
||
public static ParameterModel AddFactoryAsService(this ActionModel action, DomainModel domain, TypeModel transientType) | ||
{ | ||
var parameter = | ||
new ParameterModel(transientType, ParameterModelFrom.Services, $"new{transientType.Name.Pascalize()}") | ||
{ | ||
IsInvokeMethodParameter = false, | ||
Type = $"Func<{transientType.CSharpFriendlyFullName}>" | ||
}; | ||
|
||
action.Parameter[parameter.Name] = parameter; | ||
|
||
return parameter; | ||
} | ||
|
||
public static string BuildInitializer(this ParameterModel factoryParameter, string valueExpression) | ||
{ | ||
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter shold have mapped parameter"); } | ||
|
||
var initializer = factoryParameter.TypeModel.GetMembers().Methods.Having<InitializerAttribute>().Single(); | ||
|
||
return $"new{factoryParameter.TypeModel.Name.Pascalize()}().{initializer.Name}({valueExpression})"; | ||
} | ||
|
||
public static string BuildInitializerByIds(this ParameterModel factoryParameter, string valueExpression, | ||
bool isArray = default | ||
) | ||
{ | ||
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter shold have mapped parameter"); } | ||
|
||
var initializer = factoryParameter.TypeModel.GetMembers().Methods.Having<InitializerAttribute>().Single(); | ||
var byIds = $"{valueExpression}.Select(id => new{factoryParameter.TypeModel.Name.Pascalize()}().{initializer.Name}(id))"; | ||
|
||
return isArray | ||
? $"{byIds}.ToArray()" | ||
: $"{byIds}.ToList()"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
test/recipe/Baked.Test.Recipe.Service.Test/CodingStyle/LookingUpTransientsById.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Newtonsoft.Json; | ||
using System.Net.Http.Json; | ||
|
||
namespace Baked.Test.CodingStyle; | ||
|
||
public class LookingUpTransientsById : TestServiceNfr | ||
{ | ||
[Test] | ||
public async Task TransientParameters() | ||
{ | ||
var response = await Client.PostAsync("/method-samples/transient-parameters", JsonContent.Create( | ||
new | ||
{ | ||
transientId = "1" | ||
} | ||
)); | ||
dynamic? actual = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); | ||
|
||
$"{actual?.id}".ShouldBe("1"); | ||
} | ||
|
||
[Test] | ||
public async Task TransientListParameters() | ||
{ | ||
var response = await Client.PostAsync("/method-samples/transient-list-parameters", JsonContent.Create( | ||
new | ||
{ | ||
transientIds = new[] { "1", "2" }, | ||
otherTransientIds = new[] { "3", "4" }, | ||
} | ||
)); | ||
dynamic? actual = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); | ||
|
||
((int?)actual?.Count).ShouldBe(4); | ||
$"{actual?[0].id}".ShouldBe("1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters