Skip to content

Commit 24fde9f

Browse files
committed
chore(Caller): Change parameters
1 parent 8cab5e1 commit 24fde9f

File tree

10 files changed

+75
-59
lines changed

10 files changed

+75
-59
lines changed

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/Attributes/RoutePatternAttribute .cs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public class RoutePatternAttribute : Attribute
88
{
99
public string Pattern { get; set; }
1010

11+
/// <summary>
12+
/// The request method, the default is null (the request method is automatically identified according to the method name prefix)
13+
/// </summary>
1114
public string? HttpMethod { get; set; }
1215

1316
public bool StartWithBaseUri { get; set; }

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/Infrastructure/ServiceBaseHelper.cs src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/Helpers/ServiceBaseHelper.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ public static Delegate CreateDelegate(MethodInfo methodInfo, object targetInstan
1515

1616
public static string CombineUris(params string[] uris) => string.Join("/", uris.Select(u => u.Trim('/')));
1717

18-
public static string TrimMethodName(string methodName)
18+
public static string TrimEndMethodName(string methodName)
1919
=> methodName.TrimEnd("Async", StringComparison.OrdinalIgnoreCase);
2020

21-
public static bool TryParseHttpMethod(string[] prefixs, ref string methodName)
21+
public static string ParseMethodPrefix(string[] prefixes, string methodName)
2222
{
2323
var newMethodName = methodName;
24-
var prefix = prefixs.FirstOrDefault(prefix => newMethodName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
24+
var prefix = prefixes.FirstOrDefault(prefix => newMethodName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
2525

2626
if (prefix is not null)
27-
{
28-
methodName = methodName.Substring(prefix.Length);
29-
return true;
30-
}
31-
return false;
27+
return prefix;
28+
29+
return string.Empty;
3230
}
3331
}

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Original usage:
77
```C#
88
var builder = WebApplication.CreateBuilder(args);
99
var app = builder.Build();
10-
app.MapGet("/api/v1/IntegrationEvent/HelloWorld", () => "Hello World");
10+
app.MapGet("/api/v1/Demo/HelloWorld", () => "Hello World");
1111
app.Run();
1212
```
1313

@@ -27,7 +27,7 @@ var app = builder.Services.AddServices(builder);
2727
2. Custom Service and inherit ServiceBase
2828

2929
```c#
30-
public class IntegrationEventService : ServiceBase
30+
public class DemoService : ServiceBase
3131
{
3232
public string HelloWorld()
3333
{

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/README.zh-CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
```C#
88
var builder = WebApplication.CreateBuilder(args);
99
var app = builder.Build();
10-
app.MapGet("/api/v1/IntegrationEvent/HelloWorld", () => "Hello World");
10+
app.MapGet("/api/v1/Demo/HelloWorld", () => "Hello World");
1111
app.Run();
1212
```
1313

@@ -27,7 +27,7 @@ var app = builder.Services.AddServices(builder);
2727
2. 自定义Service并继承ServiceBase,如:
2828

2929
```c#
30-
public class IntegrationEventService : ServiceBase
30+
public class DemoService : ServiceBase
3131
{
3232
public string HelloWorld()
3333
{

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceBase.cs

+45-29
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ public abstract class ServiceBase : IService
99

1010
public string BaseUri { get; init; }
1111

12-
public ServiceRouteOptions Route { get; } = new();
12+
public ServiceRouteOptions RouteOptions { get; } = new();
1313

1414
public string? ServiceName { get; init; }
1515

16+
/// <summary>
17+
/// Based on the RouteHandlerBuilder extension, it is used to extend the mapping method, such as
18+
/// RouteHandlerBuilder = routeHandlerBuilder =>
19+
/// {
20+
/// routeHandlerBuilder.RequireAuthorization("AtLeast21");
21+
/// };
22+
/// </summary>
1623
public Action<RouteHandlerBuilder>? RouteHandlerBuilder { get; init; }
1724

1825
public IServiceCollection Services => MasaApp.Services;
@@ -54,20 +61,24 @@ internal void AutoMapRoute(ServiceGlobalRouteOptions globalOptions, Pluralizatio
5461
string? pattern = null;
5562
string? httpMethod = null;
5663
string? methodName = null;
57-
var atttibute = method.GetCustomAttribute<RoutePatternAttribute>();
58-
if (atttibute != null)
64+
var attribute = method.GetCustomAttribute<RoutePatternAttribute>();
65+
if (attribute != null)
5966
{
60-
httpMethod = atttibute.HttpMethod;
61-
if (atttibute.StartWithBaseUri)
62-
methodName = atttibute.Pattern;
67+
httpMethod = attribute.HttpMethod;
68+
if (attribute.StartWithBaseUri)
69+
methodName = attribute.Pattern;
6370
else
64-
pattern = atttibute.Pattern;
71+
pattern = attribute.Pattern;
6572
}
6673

6774
string newMethodName = method.Name;
6875

6976
if (httpMethod == null || pattern == null)
70-
httpMethod ??= TryGetHttpMethod(globalOptions, ref newMethodName);
77+
{
78+
var result = ParseMethod(globalOptions, newMethodName);
79+
httpMethod ??= result.HttpMethod;
80+
newMethodName = result.MethodName;
81+
}
7182

7283
pattern ??= ServiceBaseHelper.CombineUris(GetBaseUri(globalOptions, pluralizationService),
7384
methodName ?? GetMethodName(method, newMethodName, globalOptions));
@@ -91,9 +102,10 @@ protected virtual string GetBaseUri(ServiceRouteOptions globalOptions, Pluraliza
91102

92103
var list = new List<string>()
93104
{
94-
Route.Prefix ?? globalOptions.Prefix ?? string.Empty,
95-
Route.Version ?? globalOptions.Version ?? string.Empty,
96-
ServiceName ?? GetServiceName(Route.PluralizeServiceName ?? globalOptions.PluralizeServiceName ?? false ? pluralizationService :
105+
RouteOptions.Prefix ?? globalOptions.Prefix ?? string.Empty,
106+
RouteOptions.Version ?? globalOptions.Version ?? string.Empty,
107+
ServiceName ?? GetServiceName(RouteOptions.PluralizeServiceName ?? globalOptions.PluralizeServiceName ?? false ?
108+
pluralizationService :
97109
null)
98110
};
99111

@@ -112,34 +124,38 @@ private string GetServiceName(PluralizationService? pluralizationService)
112124
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
113125
protected virtual string GetMethodName(MethodInfo methodInfo, string methodName, ServiceRouteOptions globalOptions)
114126
{
115-
if (!(Route.AutoAppendId ?? globalOptions.AutoAppendId ?? false))
116-
return ServiceBaseHelper.TrimMethodName(methodName);
127+
if (!(RouteOptions.AutoAppendId ?? globalOptions.AutoAppendId ?? false))
128+
return ServiceBaseHelper.TrimEndMethodName(methodName);
117129

118130
var idParameter = methodInfo.GetParameters().FirstOrDefault(p => p.Name!.Equals("id", StringComparison.OrdinalIgnoreCase));
119131
if (idParameter != null)
120132
{
121133
var id = idParameter.ParameterType.IsNullableType() || idParameter.HasDefaultValue ? "{id?}" : "{id}";
122-
return $"{ServiceBaseHelper.TrimMethodName(methodName)}/{id}";
134+
return $"{ServiceBaseHelper.TrimEndMethodName(methodName)}/{id}";
123135
}
124136

125-
return ServiceBaseHelper.TrimMethodName(methodName);
137+
return ServiceBaseHelper.TrimEndMethodName(methodName);
126138
}
127139

128-
protected virtual string? TryGetHttpMethod(ServiceRouteOptions globalOptions, ref string methodName)
140+
protected virtual (string? HttpMethod, string MethodName) ParseMethod(ServiceRouteOptions globalOptions, string methodName)
129141
{
130-
if (ServiceBaseHelper.TryParseHttpMethod(Route.GetPrefixs ?? globalOptions.GetPrefixs!, ref methodName))
131-
return "GET";
142+
var prefix = ServiceBaseHelper.ParseMethodPrefix(RouteOptions.GetPrefixes ?? globalOptions.GetPrefixes!, methodName);
143+
if (!string.IsNullOrEmpty(prefix))
144+
return ("GET", methodName.Substring(prefix.Length));
132145

133-
if (ServiceBaseHelper.TryParseHttpMethod(Route.PostPrefixs ?? globalOptions.PostPrefixs!, ref methodName))
134-
return "POST";
146+
prefix = ServiceBaseHelper.ParseMethodPrefix(RouteOptions.PostPrefixes ?? globalOptions.PostPrefixes!, methodName);
147+
if (!string.IsNullOrEmpty(prefix))
148+
return ("POST", methodName.Substring(prefix.Length));
135149

136-
if (ServiceBaseHelper.TryParseHttpMethod(Route.PutPrefixs ?? globalOptions.PutPrefixs!, ref methodName))
137-
return "PUT";
150+
prefix = ServiceBaseHelper.ParseMethodPrefix(RouteOptions.PutPrefixes ?? globalOptions.PutPrefixes!, methodName);
151+
if (!string.IsNullOrEmpty(prefix))
152+
return ("PUT", methodName.Substring(prefix.Length));
138153

139-
if (ServiceBaseHelper.TryParseHttpMethod(Route.DeletePrefixs ?? globalOptions.DeletePrefixs!, ref methodName))
140-
return "DELETE";
154+
prefix = ServiceBaseHelper.ParseMethodPrefix(RouteOptions.DeletePrefixes ?? globalOptions.DeletePrefixes!, methodName);
155+
if (!string.IsNullOrEmpty(prefix))
156+
return ("DELETE", methodName.Substring(prefix.Length));
141157

142-
return null;
158+
return (null, string.Empty);
143159
}
144160

145161
#region Obsolete
@@ -171,7 +187,7 @@ protected ServiceBase(IServiceCollection services, string baseUri) : this(servic
171187
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
172188
protected RouteHandlerBuilder MapGet(Delegate handler, string? customUri = null, bool trimEndAsync = true)
173189
{
174-
customUri ??= ServiceBaseHelper.TrimMethodName(handler.Method.Name);
190+
customUri ??= ServiceBaseHelper.TrimEndMethodName(handler.Method.Name);
175191

176192
var pattern = ServiceBaseHelper.CombineUris(BaseUri, customUri);
177193

@@ -190,7 +206,7 @@ protected RouteHandlerBuilder MapGet(Delegate handler, string? customUri = null,
190206
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
191207
protected RouteHandlerBuilder MapPost(Delegate handler, string? customUri = null, bool trimEndAsync = true)
192208
{
193-
customUri ??= ServiceBaseHelper.TrimMethodName(handler.Method.Name);
209+
customUri ??= ServiceBaseHelper.TrimEndMethodName(handler.Method.Name);
194210

195211
var pattern = ServiceBaseHelper.CombineUris(BaseUri, customUri);
196212

@@ -209,7 +225,7 @@ protected RouteHandlerBuilder MapPost(Delegate handler, string? customUri = null
209225
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
210226
protected RouteHandlerBuilder MapPut(Delegate handler, string? customUri = null, bool trimEndAsync = true)
211227
{
212-
customUri ??= ServiceBaseHelper.TrimMethodName(handler.Method.Name);
228+
customUri ??= ServiceBaseHelper.TrimEndMethodName(handler.Method.Name);
213229

214230
var pattern = ServiceBaseHelper.CombineUris(BaseUri, customUri);
215231

@@ -228,7 +244,7 @@ protected RouteHandlerBuilder MapPut(Delegate handler, string? customUri = null,
228244
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
229245
protected RouteHandlerBuilder MapDelete(Delegate handler, string? customUri = null, bool trimEndAsync = true)
230246
{
231-
customUri ??= ServiceBaseHelper.TrimMethodName(handler.Method.Name);
247+
customUri ??= ServiceBaseHelper.TrimEndMethodName(handler.Method.Name);
232248

233249
var pattern = ServiceBaseHelper.CombineUris(BaseUri, customUri);
234250

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static WebApplication AddServices(
7070
services.AddServices<ServiceBase>(true, (_, serviceInstance) =>
7171
{
7272
var instance = (ServiceBase)serviceInstance;
73-
if (instance.Route.DisableAutoMapRoute ?? serviceMapOptions.DisableAutoMapRoute ?? false)
73+
if (instance.RouteOptions.DisableAutoMapRoute ?? serviceMapOptions.DisableAutoMapRoute ?? false)
7474
return;
7575

7676
instance.AutoMapRoute(serviceMapOptions, serviceMapOptions.Pluralization);

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceGlobalRouteOptions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public ServiceGlobalRouteOptions()
1818
Version = "v1";
1919
AutoAppendId = true;
2020
PluralizeServiceName = true;
21-
GetPrefixs = new[] { "Get", "Select" };
22-
PostPrefixs = new[] { "Post", "Add", "Upsert", "Create" };
23-
PutPrefixs = new[] { "Put", "Update", "Modify" };
24-
DeletePrefixs = new[] { "Delete", "Remove" };
21+
GetPrefixes = new[] { "Get", "Select" };
22+
PostPrefixes = new[] { "Post", "Add", "Upsert", "Create" };
23+
PutPrefixes = new[] { "Put", "Update", "Modify" };
24+
DeletePrefixes = new[] { "Delete", "Remove" };
2525
Assemblies = AppDomain.CurrentDomain.GetAssemblies();
2626
Pluralization = PluralizationService.CreateService(CultureInfo.CreateSpecificCulture("en"));
2727
}

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceRouteOptions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class ServiceRouteOptions
2323

2424
public bool? PluralizeServiceName { get; set; }
2525

26-
public string[]? GetPrefixs { get; set; }
26+
public string[]? GetPrefixes { get; set; }
2727

28-
public string[]? PostPrefixs { get; set; }
28+
public string[]? PostPrefixes { get; set; }
2929

30-
public string[]? PutPrefixs { get; set; }
30+
public string[]? PutPrefixes { get; set; }
3131

32-
public string[]? DeletePrefixs { get; set; }
32+
public string[]? DeletePrefixes { get; set; }
3333
}

src/Contrib/Service/Tests/Masa.Contrib.Service.MinimalAPIs.Tests/ServiceBaseTest.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void TestGetBaseUri()
2626
[DataRow("Order/Get", "Order/Get")]
2727
public void TestFormatMethods(string methodName, string result)
2828
{
29-
Assert.AreEqual(result, ServiceBaseHelper.TrimMethodName(methodName));
29+
Assert.AreEqual(result, ServiceBaseHelper.TrimEndMethodName(methodName));
3030
}
3131

3232
[TestMethod]
@@ -42,13 +42,12 @@ public void TestCombineUris()
4242
}
4343

4444
[DataTestMethod]
45-
[DataRow("Update,Modify,Put", "AddGoods", "AddGoods", false)]
46-
[DataRow("Add, Upsert, Create, AddGoods", "AddGoods", "Goods", true)]
47-
public void TestTryParseHttpMethod(string prefixs, string methodName, string newMethodName, bool exist)
45+
[DataRow("Update,Modify,Put", "AddGoods", "")]
46+
[DataRow("Add, Upsert, Create, AddGoods", "AddGoods", "Add")]
47+
public void TestTryParseHttpMethod(string prefixes, string methodName, string prefix)
4848
{
49-
var result = ServiceBaseHelper.TryParseHttpMethod(prefixs.Split(','), ref methodName);
50-
Assert.AreEqual(exist, result);
51-
Assert.AreEqual(newMethodName, methodName);
49+
var result = ServiceBaseHelper.ParseMethodPrefix(prefixes.Split(','), methodName);
50+
Assert.AreEqual(prefix, result);
5251
}
5352

5453
#region private methods

src/Contrib/Service/Tests/Masa.Contrib.Service.MinimalAPIs.Tests/Services/GoodsService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class GoodsService : CustomServiceBase
77
{
88
public GoodsService()
99
{
10-
Route.Prefix = "api";
11-
Route.Version = "v2";
10+
RouteOptions.Prefix = "api";
11+
RouteOptions.Version = "v2";
1212
}
1313
}

0 commit comments

Comments
 (0)