refactor(data): 重构数据存储库的键生成逻辑

- 将 DataRepository 中的 GetKey 方法从私有改为受保护的虚拟方法
- 简化了基于类型的键生成逻辑,使用 FullName 替代 Name 和前缀组合
- 移除了 DataRepositoryOptions 中的 KeyPrefix 属性
- 将 UnifiedSettingsRepository 中的相关方法改为受保护的虚拟方法
- 简化了统一键生成的路径处理逻辑,移除多余的 TrimEnd 操作
- [release ci]
This commit is contained in:
GeWuYou 2026-01-29 23:15:17 +08:00
parent cc8f40ee44
commit 49d210b9ad
3 changed files with 7 additions and 18 deletions

View File

@ -23,11 +23,6 @@ public class DataRepositoryOptions
/// </summary> /// </summary>
public string BasePath { get; set; } = ""; public string BasePath { get; set; } = "";
/// <summary>
/// 键名前缀(如 "Game",生成的键为 "Game_SettingsData"
/// </summary>
public string KeyPrefix { get; set; } = "";
/// <summary> /// <summary>
/// 是否在保存时自动备份 /// 是否在保存时自动备份
/// </summary> /// </summary>

View File

@ -162,7 +162,7 @@ public class DataRepository(IStorage? storage, DataRepositoryOptions? options =
/// </summary> /// </summary>
/// <typeparam name="T">数据类型</typeparam> /// <typeparam name="T">数据类型</typeparam>
/// <returns>生成的存储键</returns> /// <returns>生成的存储键</returns>
private string GetKey<T>() where T : IData protected virtual string GetKey<T>() where T : IData
{ {
return GetKey(typeof(T)); return GetKey(typeof(T));
} }
@ -172,13 +172,9 @@ public class DataRepository(IStorage? storage, DataRepositoryOptions? options =
/// </summary> /// </summary>
/// <param name="type">数据类型</param> /// <param name="type">数据类型</param>
/// <returns>生成的存储键</returns> /// <returns>生成的存储键</returns>
private string GetKey(Type type) protected virtual string GetKey(Type type)
{ {
var fileName = $"{_options.KeyPrefix}_{type.Name}"; var fileName = type.FullName!;
return string.IsNullOrEmpty(_options.BasePath) ? fileName : $"{_options.BasePath}/{fileName}";
if (string.IsNullOrEmpty(_options.BasePath))
return fileName;
var basePath = _options.BasePath.TrimEnd('/');
return $"{basePath}/{fileName}";
} }
} }

View File

@ -219,10 +219,9 @@ public class UnifiedSettingsRepository(
/// 获取统一文件的存储键名 /// 获取统一文件的存储键名
/// </summary> /// </summary>
/// <returns>完整的存储键名</returns> /// <returns>完整的存储键名</returns>
private string GetUnifiedKey() protected virtual string GetUnifiedKey()
{ {
var name = string.IsNullOrEmpty(_options.KeyPrefix) ? fileName : $"{_options.KeyPrefix}_{fileName}"; return string.IsNullOrEmpty(_options.BasePath) ? fileName : $"{_options.BasePath}/{fileName}";
return string.IsNullOrEmpty(_options.BasePath) ? name : $"{_options.BasePath.TrimEnd('/')}/{name}";
} }
/// <summary> /// <summary>
@ -230,9 +229,8 @@ public class UnifiedSettingsRepository(
/// </summary> /// </summary>
/// <param name="type">要获取键的类型</param> /// <param name="type">要获取键的类型</param>
/// <returns>类型的全名作为键</returns> /// <returns>类型的全名作为键</returns>
private static string GetTypeKey(Type type) protected virtual string GetTypeKey(Type type)
{ {
return type.FullName!; return type.FullName!;
// ⚠️ 刻意不用 AssemblyQualifiedName
} }
} }