Giter Club home page Giter Club logo

Comments (18)

ardalis avatar ardalis commented on June 29, 2024 4

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.

rafaelsc avatar rafaelsc commented on June 29, 2024 2

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.

tormodfj avatar tormodfj commented on June 29, 2024 2

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.

ardalis avatar ardalis commented on June 29, 2024 2

Trying to deploy 4.0.1 now.

from guardclauses.

ardalis avatar ardalis commented on June 29, 2024 1

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.

ardalis avatar ardalis commented on June 29, 2024 1

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.

djeikyb avatar djeikyb commented on June 29, 2024 1

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.

ardalis avatar ardalis commented on June 29, 2024 1

I agree it should be added throughout. Open to PRs, or I'll get to it at some point.

from guardclauses.

tormodfj avatar tormodfj commented on June 29, 2024

This article in CODE Magazine has a section explaining the use of [CallerArgumentExpression]:
Essential C# 10.0: Making it Simpler

from guardclauses.

DickBaker avatar DickBaker commented on June 29, 2024

afaik that extension idea syntax should be [added =null!]
public static T NotNullOrWhiteSpace(this T input, string message, [CallerArgumentExpression] parameterName = null!);

from guardclauses.

stap123 avatar stap123 commented on June 29, 2024

related to #149

from guardclauses.

ardalis avatar ardalis commented on June 29, 2024

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.

danielmackay avatar danielmackay commented on June 29, 2024

@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.

danielmackay avatar danielmackay commented on June 29, 2024

@ardalis Any guidance on contributing via PR? Feature branch on this repo? Or PR from a branch on a forked repo?

from guardclauses.

ardalis avatar ardalis commented on June 29, 2024

Fork, branch, make changes, create PR.

from guardclauses.

stap123 avatar stap123 commented on June 29, 2024

@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.

stap123 avatar stap123 commented on June 29, 2024

ping @ardalis any chance of pushing that release soon? Sorry to nag 😕 thanks for all the work 😃

from guardclauses.

ardalis avatar ardalis commented on June 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.