Comments (18)
Hmm, I just did a release < 3 weeks ago. Wonder how that got missed / came in after. I'll try and push out a point release this week then, I guess.
from guardclauses.
The biggest issue of implementation of this feature in GuardClauses is that can cause confusion with the message
and parameterName
. When omitting the parameterName in the attribute list.
_name = Guard.Against.NullOrWhiteSpace(name, nameof(name), "Name is Invalid!");
_name = Guard.Against.NullOrWhiteSpace(name, "Name is Invalid!");
// BUG - This will set the parameterName to "Name is Invalid!", not the error message.
_name = Guard.Against.NullOrWhiteSpace(name, message: "Name is Invalid!""); // OK
from guardclauses.
Is that improved if we use an extension?
_name = name.NotNullOrWhiteSpace("Name can't be empty"); // extension idea - not tested just writing in browser... public static T NotNullOrWhiteSpace(this T input, string message, [CallerArgumentExpression] parameterName);
To further prevent any overload resolution issues, one could place such extension methods in a separate namespace, making the experience opt-in from a developer's point of view.
from guardclauses.
Trying to deploy 4.0.1 now.
from guardclauses.
Yeah, here's another one.
https://weblogs.asp.net/dixin/csharp-10-new-feature-callerargumentexpression-argument-check-and-more
I think it will make a good alternate syntax, but I want to keep the previous syntax as well for at least one version so it doesn't break the world.
from guardclauses.
Is that improved if we use an extension?
_name = name.NotNullOrWhiteSpace("Name can't be empty");
// extension idea - not tested just writing in browser...
public static T NotNullOrWhiteSpace(this T input, string message, [CallerArgumentExpression] parameterName);
from guardclauses.
Not for null, but I'm missing the [CallerArgumentExpression]
for the rest of the extension methods, like NullOrEmpty
and Negative
. I see them on main; looking forward to their release!
from guardclauses.
I agree it should be added throughout. Open to PRs, or I'll get to it at some point.
from guardclauses.
This article in CODE Magazine has a section explaining the use of [CallerArgumentExpression]
:
Essential C# 10.0: Making it Simpler
from guardclauses.
afaik that extension idea syntax should be [added =null!]
public static T NotNullOrWhiteSpace(this T input, string message, [CallerArgumentExpression] parameterName = null!);
from guardclauses.
related to #149
from guardclauses.
The latest v4 version includes this code:
#if NETSTANDARD || NETFRAMEWORK
public static T Null<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static T Null<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
if (input is null)
{
if (string.IsNullOrEmpty(message))
{
throw new ArgumentNullException(parameterName);
}
throw new ArgumentNullException(parameterName, message);
}
return input;
}
Anyone running into issues with method resolution using this? All unit tests seem to work fine.
from guardclauses.
@djeikyb same here. I've just updated to V4 and thought this would have been added throughout. Would be a very nice addition to get this functionality across the board.
from guardclauses.
@ardalis Any guidance on contributing via PR? Feature branch on this repo? Or PR from a branch on a forked repo?
from guardclauses.
Fork, branch, make changes, create PR.
from guardclauses.
@ardalis It's already been merged, we did the PR a while ago just wasn't released 🥲 It's sat in master waiting for a release 😄
from guardclauses.
ping @ardalis any chance of pushing that release soon? Sorry to nag 😕 thanks for all the work 😃
from guardclauses.
GitHub action failed but the publish worked - 4.0.1 should be available momentarily!
https://github.com/ardalis/GuardClauses/runs/5723074640?check_suite_focus=true
from guardclauses.
Related Issues (20)
- Enhanced support for value types HOT 3
- Feature request: GuardComplete rider add-in HOT 1
- Support passing ParamName when using GuardAgainstExpression
- Make T nullable HOT 2
- Assign pull-requests: write permission to build action HOT 6
- Images
- Re-add JetBrains Annotations HOT 1
- CallerArgumentExpression No Longer Working When Targeting net6.0 HOT 9
- Localization and multi language support HOT 1
- Fix `AgainstExpression` to be `Expression` HOT 2
- Support .NET 6, .NET 7 AND .NET 8? HOT 1
- FluentValidation
- MaxLengthExceeded / TooLong HOT 2
- MinLength / TooShort
- LengthOutOfRange HOT 3
- Fix Build HOT 1
- Unify method signatures among frameworks HOT 2
- Can we support Expression on refrence type not only struct
- Typo in the StringTooLong error message HOT 2
- Namespace typo? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guardclauses.