[Feature] Extend Guard Methods to Return Validated Arguments
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- csharp
- Área
- developer-experience
Línea de trabajo
Empieza revisando la Guard API existente y sus métodos; después, compara los enfoques propuestos ResultGuard y modified-Guard. Confirma con los maintainers el comportamiento de retorno previsto y la compatibilidad de la API; se considera terminado cuando los métodos Guard acordados devuelven argumentos validados sin cambiar los fallos de validación ni el uso existente.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- C#
- Estrellas
- 3.8k
- Forks
- 401
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de CommunityToolkit/dotnet
-
bug :bug:
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
CommunityToolkit/dotnet#1206 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
Dificultad 1/5 Menos de una hora Aptitud para principiantes 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
CommunityToolkit/dotnet#1215 ·
-
feature request :mailbox_with_mail:
Dificultad 5/5 Más de una semana Aptitud para principiantes 45/100
CommunityToolkit/dotnet#1214 ·
Todos los issues de CommunityToolkit/dotnet
Issues similares
-
bug P3
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
nightscout/nocturne#1908 ·
Los mantenedores suelen responder en 1 día
-
bug documentation Needs: Triage :mag:
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
WPF: each page's `Title` overwrites the window title, and returning to a page does not restore itAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
agentic-workflows area/Docs partner/agentic-workflows
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
jamesmontemagno/tiny-clips#378 · 2 comentarios ·
Los mantenedores suelen responder en 1 día