Skip to content

Commit 941862a

Browse files
committed
refactor!: use enum to present map nullable route parameter
1 parent b340d1a commit 941862a

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</Description>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0"/>
11-
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0"/>
10+
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
11+
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
1212
</ItemGroup>
1313
<ItemGroup>
14-
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.Abstractions\Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj"/>
15-
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.csproj"/>
14+
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.Abstractions\Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj" />
15+
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.csproj" />
1616
</ItemGroup>
1717
</Project>

src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsRouteMapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static class CqrsRouteMapper
4343
public static IEndpointConventionBuilder MapQuery<T>(
4444
this IEndpointRouteBuilder app,
4545
[StringSyntax("Route")] string route,
46-
bool mapNullableRouteParameters = false,
46+
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter.Disable,
4747
string nullRouteParameterPattern = "-")
4848
{
4949
return app.MapQuery(
@@ -79,7 +79,7 @@ public static IEndpointConventionBuilder MapQuery(
7979
this IEndpointRouteBuilder app,
8080
[StringSyntax("Route")] string route,
8181
Delegate handler,
82-
bool mapNullableRouteParameters = false,
82+
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter.Disable,
8383
string nullRouteParameterPattern = "-")
8484
{
8585
var isQuery = handler.Method.ReturnType.GetInterfaces().Where(x => x.IsGenericType)
@@ -90,7 +90,7 @@ public static IEndpointConventionBuilder MapQuery(
9090
"delegate does not return a query, please make sure it returns object that implement IQuery<> or IListQuery<> or interface that inherit from them");
9191
}
9292

93-
if (mapNullableRouteParameters == false)
93+
if (mapNullableRouteParameters is MapNullableRouteParameter.Disable)
9494
{
9595
return app.MapGet(route, handler).AddEndpointFilter<QueryEndpointHandler>();
9696
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
2+
3+
/// <summary>
4+
/// Defines behavior for nullable route parameters.
5+
/// </summary>
6+
public enum MapNullableRouteParameter
7+
{
8+
/// <summary>
9+
/// Map different routes to present <c>null</c> for nullable route parameters.
10+
/// </summary>
11+
Enable = 1,
12+
13+
/// <summary>
14+
/// Do not map extra route for nullable route parameters.
15+
/// </summary>
16+
Disable = 2
17+
}

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
var apis = app.NewVersionedApi();
3737
var v1 = apis.MapGroup("/api/v{version:apiVersion}").HasApiVersion(1);
38-
v1.MapQuery<GetStringQuery>("apps/{appId}/strings/{stringId:int}/value", true);
38+
v1.MapQuery<GetStringQuery>("apps/{appId}/strings/{stringId:int}/value", MapNullableRouteParameter.Enable);
3939
v1.MapQuery<GetStringQuery>("strings/{id:int}");
4040
v1.MapQuery<ListStringsQuery>("strings");
4141
v1.MapCommand("strings", (CreatePayload payload) => new CreateCommand(payload.NeedError));

0 commit comments

Comments
 (0)