mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
feat(core): 添加函数式编程扩展和依赖注入容器实现
- 实现 ResultExtensions 扩展类,提供 Combine、Map、Bind 等函数式操作 - 添加 MicrosoftDiContainer 依赖注入容器,支持单例、瞬态、作用域服务注册 - 实现 CQRS 管道行为和处理器注册功能 - 添加本地化字符串实现和相关工具方法 - 提供线程安全的多读单写锁保护机制 - 实现服务实例的优先级排序和批量获取功能
This commit is contained in:
parent
23489570bf
commit
3f25ea5624
@ -177,7 +177,7 @@ public static class ResultExtensions
|
||||
return result.Match(
|
||||
succ: value => predicate(value)
|
||||
? result
|
||||
: Result<T>.Fail(new ArgumentException(errorMessage, nameof(errorMessage))),
|
||||
: Result<T>.Fail(new ArgumentException(errorMessage)),
|
||||
fail: _ => result
|
||||
);
|
||||
}
|
||||
|
||||
@ -394,7 +394,7 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
||||
{
|
||||
if (assembly is null)
|
||||
{
|
||||
throw new ArgumentException("Assemblies collection cannot contain null items.", nameof(assemblies));
|
||||
throw new ArgumentNullException(nameof(assemblies), "Assemblies collection cannot contain null items.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,11 +8,26 @@ namespace GFramework.Core.Localization;
|
||||
/// </summary>
|
||||
public class LocalizationString : ILocalizationString
|
||||
{
|
||||
/// <summary>
|
||||
/// 正则分组名:变量名。
|
||||
/// </summary>
|
||||
private const string VariableGroupName = "variable";
|
||||
|
||||
/// <summary>
|
||||
/// 正则分组名:格式化器名。
|
||||
/// </summary>
|
||||
private const string FormatterGroupName = "formatter";
|
||||
|
||||
/// <summary>
|
||||
/// 正则分组名:格式化器参数。
|
||||
/// </summary>
|
||||
private const string FormatterArgsGroupName = "args";
|
||||
|
||||
/// <summary>
|
||||
/// 匹配 {variableName} 或 {variableName:formatter:args} 的正则表达式模式
|
||||
/// </summary>
|
||||
private const string FormatVariablePattern =
|
||||
@"\{([a-zA-Z_][a-zA-Z0-9_]*)(?::([a-zA-Z_][a-zA-Z0-9_]*)(?::([^}]+))?)?\}";
|
||||
@"\{(?<variable>[a-zA-Z_][a-zA-Z0-9_]*)(?::(?<formatter>[a-zA-Z_][a-zA-Z0-9_]*)(?::(?<args>[^}]+))?)?\}";
|
||||
|
||||
/// <summary>
|
||||
/// 预编译的静态正则表达式,用于格式化字符串中的变量替换
|
||||
@ -157,13 +172,13 @@ public class LocalizationString : ILocalizationString
|
||||
Dictionary<string, object> variables,
|
||||
ILocalizationManager manager)
|
||||
{
|
||||
var variableName = match.Groups[1].Value;
|
||||
var variableName = match.Groups[VariableGroupName].Value;
|
||||
if (!variables.TryGetValue(variableName, out var value))
|
||||
{
|
||||
return match.Value;
|
||||
}
|
||||
|
||||
var formatterName = GetOptionalGroupValue(match, 2);
|
||||
var formatterName = GetOptionalGroupValue(match, FormatterGroupName);
|
||||
if (string.IsNullOrEmpty(formatterName))
|
||||
{
|
||||
return FormatValue(value, manager);
|
||||
@ -190,7 +205,7 @@ public class LocalizationString : ILocalizationString
|
||||
ILocalizationManager manager,
|
||||
out string result)
|
||||
{
|
||||
var formatterArgs = GetOptionalGroupValue(match, 3) ?? string.Empty;
|
||||
var formatterArgs = GetOptionalGroupValue(match, FormatterArgsGroupName) ?? string.Empty;
|
||||
if (GetFormatter(manager, formatterName) is { } formatter &&
|
||||
formatter.TryFormat(formatterArgs, value, manager.CurrentCulture, out result))
|
||||
{
|
||||
@ -220,11 +235,11 @@ public class LocalizationString : ILocalizationString
|
||||
/// 获取正则表达式匹配组中的可选值
|
||||
/// </summary>
|
||||
/// <param name="match">正则表达式匹配结果</param>
|
||||
/// <param name="groupIndex">要获取的组索引</param>
|
||||
/// <param name="groupName">要获取的命名组</param>
|
||||
/// <returns>如果该组匹配成功则返回其值;否则返回 null</returns>
|
||||
private static string? GetOptionalGroupValue(Match match, int groupIndex)
|
||||
private static string? GetOptionalGroupValue(Match match, string groupName)
|
||||
{
|
||||
return match.Groups[groupIndex].Success ? match.Groups[groupIndex].Value : null;
|
||||
return match.Groups[groupName].Success ? match.Groups[groupName].Value : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user