XFEExtension.NetCore.ServerInteractive
[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFEExtension.NetCore.ServerInteractive.SourceGenerator/EntryPointGenerator.cs
+11
-44
Modified
XFEExtension.NetCore.ServerInteractive.TServer/Profiles/DataProfile.cs
+6
-6
Modified
XFEExtension.NetCore.ServerInteractive.TServer/Profiles/UserProfile.cs
+6
-6
Modified
XFEExtension.NetCore.ServerInteractive.TServer/Program.cs
+0
-1
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Helpers/RouteMatchHelper.cs
+1
-9
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs
+6
-9
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCoreBuilder.cs
+1
-1
Modified
XFEExtension.NetCore.ServerInteractive/XFEExtension.NetCore.ServerInteractive.csproj
+9
-4
XFEstudio/XFEExtension.NetCore.ServerInteractive
EntryPointGenerator与路由相关代码优化重构
- 用 LINQ 优化 EntryPointGenerator 生成逻辑,提升可读性 - 路由通配符校验与去重逻辑重构,减少冗余 - ProfileList 字段统一加下划线前缀,规范命名 - RouteMatchHelper 路由匹配改为 LINQ 实现 - XFEServerCore 路由查找与属性声明优化 - 移除无用 using,参数校验代码更简洁 - 主要为风格和一致性优化,部分提升性能与灵活性
9824abc
代码差异
8 个文件
+40
-80
@@ -132,31 +132,9 @@ public class EntryPointGenerator : IIncrementalGenerator
132
132
? LocationInfo.From(classDeclaration.Identifier.GetLocation())
133
133
: methodLocation;
134
134
135
var results = new List<MethodCandidate>();
135
var results = (from attr in entryPointAttributes select attr.ConstructorArguments.FirstOrDefault().Value?.ToString() into rawPath select string.IsNullOrEmpty(rawPath) ? "*" : rawPath!.Trim('/') into path select new MethodCandidate(containingType.ContainingNamespace.ToDisplayString(), containingType.Name, methodSymbol.Name, path, isAsync, isContainingTypePartial, methodSymbol.Parameters.Length, hasValidReturnType, returnType.ToDisplayString(), methodLocation, classLocation, typeParameters, typeConstraints)).ToList();
136
136
137
foreach (var attr in entryPointAttributes)
138
{
139
var rawPath = attr.ConstructorArguments.FirstOrDefault().Value?.ToString();
140
// 空路径或 "*" 均视为全匹配通配符;非空路径去除首尾 '/' 以与运行时路由格式对齐
141
var path = string.IsNullOrEmpty(rawPath) ? "*" : rawPath!.Trim('/');
142
143
results.Add(new MethodCandidate(
144
containingType.ContainingNamespace.ToDisplayString(),
145
containingType.Name,
146
methodSymbol.Name,
147
path!,
148
isAsync,
149
isContainingTypePartial,
150
methodSymbol.Parameters.Length,
151
hasValidReturnType,
152
returnType.ToDisplayString(),
153
methodLocation,
154
classLocation,
155
typeParameters,
156
typeConstraints));
157
}
158
159
return results.Count == 0 ? default : ImmutableArray.CreateRange(results);
137
return results.Count == 0 ? default : [..results];
160
138
}
161
139
162
140
private static void Execute(Compilation compilation, ImmutableArray<MethodCandidate> methods, SourceProductionContext context)
@@ -216,17 +194,13 @@ public class EntryPointGenerator : IIncrementalGenerator
216
194
if (method.Path.Contains("*") && method.Path != "*")
217
195
{
218
196
var segments = method.Path.Split('/');
219
foreach (var segment in segments)
197
if (segments.Any(segment => segment.Contains("*") && segment != "*"))
220
198
{
221
if (segment.Contains("*") && segment != "*")
222
{
223
context.ReportDiagnostic(Diagnostic.Create(
224
InvalidWildcardUsageRule,
225
method.MethodLocation.ToLocation(),
226
method.Path));
227
isValid = false;
228
break;
229
}
199
context.ReportDiagnostic(Diagnostic.Create(
200
InvalidWildcardUsageRule,
201
method.MethodLocation.ToLocation(),
202
method.Path));
203
isValid = false;
230
204
}
231
205
}
232
206
@@ -255,17 +229,14 @@ public class EntryPointGenerator : IIncrementalGenerator
255
229
{
256
230
if (!pathToMethods.TryGetValue(method.Path, out var list))
257
231
{
258
list = new List<MethodCandidate>();
232
list = [];
259
233
pathToMethods[method.Path] = list;
260
234
}
261
235
list.Add(method);
262
236
}
263
237
264
foreach (var kvp in pathToMethods)
238
foreach (var kvp in pathToMethods.Where(kvp => kvp.Value.Count > 1))
265
239
{
266
if (kvp.Value.Count <= 1)
267
continue;
268
269
240
hasDuplicateError = true;
270
241
foreach (var dup in kvp.Value)
271
242
{
@@ -300,13 +271,11 @@ namespace {namespaceName}
300
271
/// </summary>
301
272
public override List<string> EntryPointList {{ get; }} = new()
302
273
{{");
303
304
274
// 添加所有入口点到静态列表
305
275
foreach (var method in methodInfos)
306
276
{
307
277
sourceBuilder.AppendLine($" \"{EscapeStringLiteral(method.Path)}\",");
308
278
}
309
310
279
sourceBuilder.AppendLine($@" }};
311
280
312
281
private Dictionary<string, Action>? _generatedSyncEntryPoints;
@@ -315,13 +284,11 @@ namespace {namespaceName}
315
284
{{
316
285
get => _generatedSyncEntryPoints ??= new Dictionary<string, Action>()
317
286
{{");
318
319
287
// 添加同步入口点
320
288
foreach (var method in methodInfos.Where(m => !m.IsAsync))
321
289
{
322
290
sourceBuilder.AppendLine($" {{ \"{EscapeStringLiteral(method.Path)}\", {method.MethodName} }},");
323
291
}
324
325
292
sourceBuilder.AppendLine($@" }};
326
293
}}
327
294
@@ -352,6 +319,6 @@ namespace {namespaceName}
352
319
/// </summary>
353
320
private static string EscapeStringLiteral(string value)
354
321
{
355
return value.Replace("\\", "\\\\").Replace("\"", "\\\"");
322
return value.Replace("\\", @"\\").Replace("\"", "\\\"");
356
323
}
357
324
}
@@ -6,12 +6,12 @@ namespace XFEExtension.NetCore.ServerInteractive.TServer.Profiles;
6
6
public partial class DataProfile : XFEProfile
7
7
{
8
8
[ProfileProperty]
9
[ProfilePropertyAddGet("Current.personTable.CurrentProfile = Current")]
10
[ProfilePropertyAddGet("return Current.personTable")]
11
private ProfileList<Person> personTable = [];
9
[ProfilePropertyAddGet("Current._personTable.CurrentProfile = Current")]
10
[ProfilePropertyAddGet("return Current._personTable")]
11
private ProfileList<Person> _personTable = [];
12
12
13
13
[ProfileProperty]
14
[ProfilePropertyAddGet("Current.orderTable.CurrentProfile = Current")]
15
[ProfilePropertyAddGet("return Current.orderTable")]
16
private ProfileList<Order> orderTable = [];
14
[ProfilePropertyAddGet("Current._orderTable.CurrentProfile = Current")]
15
[ProfilePropertyAddGet("return Current._orderTable")]
16
private ProfileList<Order> _orderTable = [];
17
17
}
@@ -10,9 +10,9 @@ public partial class UserProfile : XFEProfile
10
10
/// 用户列表
11
11
/// </summary>
12
12
[ProfileProperty]
13
[ProfilePropertyAddGet("Current.userTable.CurrentProfile = Current")]
14
[ProfilePropertyAddGet("return Current.userTable")]
15
private ProfileList<User> userTable = [new()
13
[ProfilePropertyAddGet("Current._userTable.CurrentProfile = Current")]
14
[ProfilePropertyAddGet("return Current._userTable")]
15
private ProfileList<User> _userTable = [new()
16
16
{
17
17
NickName = "XFEstudio",
18
18
UserName = "Admin",
@@ -23,7 +23,7 @@ public partial class UserProfile : XFEProfile
23
23
/// 加密的用户登录列表
24
24
/// </summary>
25
25
[ProfileProperty]
26
[ProfilePropertyAddGet("Current.encryptedUserLoginModelTable.CurrentProfile = Current")]
27
[ProfilePropertyAddGet("return Current.encryptedUserLoginModelTable")]
28
private ProfileList<EncryptedUserLoginModel> encryptedUserLoginModelTable = [];
26
[ProfilePropertyAddGet("Current._encryptedUserLoginModelTable.CurrentProfile = Current")]
27
[ProfilePropertyAddGet("return Current._encryptedUserLoginModelTable")]
28
private ProfileList<EncryptedUserLoginModel> _encryptedUserLoginModelTable = [];
29
29
}
@@ -1,6 +1,5 @@
1
1
using XFEExtension.NetCore.ServerInteractive.Interfaces;
2
2
using XFEExtension.NetCore.ServerInteractive.Models.UserModels;
3
using XFEExtension.NetCore.ServerInteractive.TServer;
4
3
using XFEExtension.NetCore.ServerInteractive.TServer.Models;
5
4
using XFEExtension.NetCore.ServerInteractive.TServer.Profiles;
6
5
using XFEExtension.NetCore.ServerInteractive.TServer.Services;
@@ -34,14 +34,6 @@ internal static class RouteMatchHelper
34
34
if (patternSegments.Length != routeSegments.Length)
35
35
return false;
36
36
37
for (var i = 0; i < patternSegments.Length; i++)
38
{
39
if (patternSegments[i] == "*")
40
continue;
41
if (patternSegments[i] != routeSegments[i])
42
return false;
43
}
44
45
return true;
37
return !patternSegments.Where((t, i) => t != "*" && t != routeSegments[i]).Any();
46
38
}
47
39
}
@@ -30,7 +30,7 @@ public abstract class XFEServerCore : ServerCoreServiceBase
30
30
/// <summary>
31
31
/// 是否接收GET请求
32
32
/// </summary>
33
public bool AcceptGet { get; set; } = false;
33
public bool AcceptGet { get; set; }
34
34
/// <summary>
35
35
/// 是否接收POST请求
36
36
/// </summary>
@@ -180,10 +180,9 @@ public abstract class XFEServerCore : ServerCoreServiceBase
180
180
var stopWatch = Stopwatch.StartNew();
181
181
182
182
// 在字典中查找对应的服务(先精确匹配,再通配符匹配)
183
Func<IServerCoreStandardService>? serviceFactory = null;
184
183
string? matchedPattern = null;
185
184
186
if (StandardCoreServiceDictionary.TryGetValue(route, out serviceFactory))
185
if (StandardCoreServiceDictionary.TryGetValue(route, out var serviceFactory))
187
186
{
188
187
matchedPattern = route;
189
188
}
@@ -192,12 +191,10 @@ public abstract class XFEServerCore : ServerCoreServiceBase
192
191
// 尝试通配符匹配
193
192
foreach (var (pattern, factory) in WildcardCoreServiceList)
194
193
{
195
if (RouteMatchHelper.MatchWildcardRoute(pattern, route))
196
{
197
matchedPattern = pattern;
198
serviceFactory = factory;
199
break;
200
}
194
if (!RouteMatchHelper.MatchWildcardRoute(pattern, route)) continue;
195
matchedPattern = pattern;
196
serviceFactory = factory;
197
break;
201
198
}
202
199
}
203
200
@@ -74,7 +74,7 @@ public abstract class XFEServerCoreBuilder : XFEBuilderBase<XFEServerCoreBuilder
74
74
/// <returns>XFE服务器核心构建器</returns>
75
75
public XFEServerCoreBuilder AddServiceWithRoute<T>(string route) where T : IServerCoreStandardService, new()
76
76
{
77
ArgumentException.ThrowIfNullOrWhiteSpace(route, nameof(route));
77
ArgumentException.ThrowIfNullOrWhiteSpace(route);
78
78
route = route.Trim('/');
79
79
80
80
Func<IServerCoreStandardService> factory = () =>
@@ -5,7 +5,7 @@
5
5
<ImplicitUsings>enable</ImplicitUsings>
6
6
<Nullable>enable</Nullable>
7
7
<GenerateDocumentationFile>True</GenerateDocumentationFile>
8
<Version>3.0.2-preview.1.26046.3</Version>
8
<Version>3.0.2-preview.1.26046.4</Version>
9
9
<Title>XFEExtension.NetCore.ServerInteractive</Title>
10
10
<RepositoryUrl>https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive</RepositoryUrl>
11
11
<AnalysisLevel>latest</AnalysisLevel>
@@ -21,10 +21,15 @@
21
21
<PackageReleaseNotes>
22
22
## 调整
23
23
24
自动生成 SourceGenerator 包并优化分析器引用
24
EntryPointGenerator与路由相关代码优化重构
25
25
26
移除 SourceGenerator 项目的 <IncludeBuildOutput>,新增 <GeneratePackageOnBuild> 以支持构建时自动生成 NuGet 包。
27
调整主项目对 SourceGenerator 的 DLL 引用方式,删除静态 Release 路径引用,改为根据配置动态引用,确保打包时分析器 DLL 能正确包含。
26
- 用 LINQ 优化 EntryPointGenerator 生成逻辑,提升可读性
27
- 路由通配符校验与去重逻辑重构,减少冗余
28
- ProfileList 字段统一加下划线前缀,规范命名
29
- RouteMatchHelper 路由匹配改为 LINQ 实现
30
- XFEServerCore 路由查找与属性声明优化
31
- 移除无用 using,参数校验代码更简洁
32
- 主要为风格和一致性优化,部分提升性能与灵活性
28
33
29
34
## 新增
30
35