[Feature] Extend Guard Methods to Return Validated Arguments
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- csharp
调研方向
首先检查现有的 Guard API 及其方法,然后比较提议的 ResultGuard 和 modified-Guard 方案。与维护者确认预期的返回行为和 API 兼容性;完成的标准是,达成一致的 Guard 方法返回经过验证的参数,同时不改变验证失败行为或现有用法。
由索引模型根据 Issue 内容生成。
描述
Proposal: Extend Guard Methods to Return Validated Arguments
Summary
Extend the existing Guard functionality to provide an alternative version of all guard methods that return the validated argument when the validation passes. This enhancement would allow method chaining and inline validation assignments.
Current Usage
The current Guard methods validate an argument and throw an exception if the validation fails, but they return void. This requires an extra assignment step when the argument is needed after validation.
Example:
public class Processor
{
private readonly string _input;
public Processor(string input)
{
Guard.IsNotNullOrEmpty(input);
_input = input;
}
public void Print()
{
Console.WriteLine(_input);
}
}
Proposed Enhancement
Provide an alternative version of all guard methods that return the validated argument when the validation passes. This would allow inline validation while preserving the original API behavior.
Proposed Usage
Instead of requiring a separate assignment, the new API would allow:
public class Processor
{
private readonly string _input;
public Processor(string input)
{
_input = Guard.IsNotNullOrEmpty(input);
}
public void Print()
{
Console.WriteLine(_input);
}
}
Implementation Options
Two approaches could be taken to introduce this feature:
Option 1: Create a ResultGuard Class
Introduce a new ResultGuard class that mirrors Guard but returns the validated argument.
public static class ResultGuard
{
public static T IsNotNullOrEmpty<T>(T value, [CallerArgumentExpression("value")] string? paramName = null)
where T : class
{
Guard.IsNotNullOrEmpty(value, paramName);
return value;
}
}
Pros:
- No changes to the existing
Guardclass. - Clear separation between validation-only (
Guard) and validation-with-return (ResultGuard).
Cons:
- Code duplication or the need to refactor
Guardto reuse logic. - Users must choose between
GuardandResultGuard.
Option 2: Modify Guard to Return the Argument
Modify the existing Guard class to return the argument instead of void.
public static class Guard
{
public static T IsNotNullOrEmpty<T>(T value, [CallerArgumentExpression("value")] string? paramName = null)
where T : class
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"{paramName} cannot be null or empty.", paramName);
}
return value;
}
}
Pros:
- No need for a new class.
- Maintains a single validation API.
- Fully backward-compatible since it only extends the return type.
Cons:
- Changes the return type of existing methods, which may have unforeseen consequences in certain use cases.
Personal Preference
I personally prefer Option 2 (modifying Guard) because it avoids duplication, maintains backward compatibility, and enhances usability with minimal changes. However, I am open to other suggestions if the maintainers have concerns or alternative approaches in mind.
Would the maintainers be open to this enhancement? If so, I can submit a PR with the implementation.
- 主要语言
- C#
- 星标
- 3.8k
- 派生
- 403
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/dotnet 的其他 Issue
-
RelayCommand<T>.CanExecute(null) returns false for value types without a predicate可能已有人在做 @apachezy 于 91 天前认领。 未关闭bug :bug:
难度 2/5 1-3 小时 新手友好度 84/100
CommunityToolkit/dotnet#1206 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
难度 1/5 1 小时以内 新手友好度 68/100
CommunityToolkit/dotnet#648 ·
-
难度 5/5 一周以上 新手友好度 35/100
CommunityToolkit/dotnet#1217 ·
-
bug :bug:
难度 3/5 1-2 天 新手友好度 68/100
CommunityToolkit/dotnet#1215 · 2 条评论 ·
查看 CommunityToolkit/dotnet 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
unoplatform/uno.check#591 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
weaviate/csharp-client#378 ·
-
agentic-workflows automated testing
难度 2/5 1-3 小时 新手友好度 85/100
Azure-Samples/Legacy-Modernization-Agents#194 ·
维护者通常 1 天内回复
-
area-encryption area-security p1-recommended technical-debt
难度 2/5 1 小时以内 新手友好度 85/100
维护者通常 1 天内回复
-
V5: `asAllDay` parameter not working in `/feed/v5/calendar/sonarr.ics`可能已有人在做 @stevietv 今天认领。 未关闭needs-triage
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复