CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.99k stars 294 forks source link

Guard for IsNotEmpty(Guid) #812

Open aviita opened 9 months ago

aviita commented 9 months ago

Overview

There should be Guard.IsNotEmpty(Guid) API to Guard against empty guids. With current API's the same guard can be achieved by Guard.IsTrue(guid != Guid.Empty);.

API breakdown

namespace CommunityToolkit.Diagnostics;

public static partial class Guard
{
    public static void IsNotEmpty([DoesNotReturnIf(Guid.Empty)] Guid value, [CallerArgumentExpression(nameof(memory))] string name = "", string message = "");
}

Note that I would also prefer to override the message in many cases, but without actually writing the argument name my self, so making also the message, with default value of empty string, but would only use it for the message if not empty (maybe).

Usage example

public class MyClass { private Guid _id;

public MyClass(Guid id)
{
    Guard.IsNotEmptyGuid(id);

    _id = id;
}

}

Breaking change?

No

Alternatives

Guard.IsTrue(guid == Guid.Empty);

Additional context

We are working with guids a lot in our project and would like to have cleaner guard for empty guids than Guard.IsTrue(guid == Guid.Empty);.

Verifying for empty Guid is pointless to have, so we only need guard for IsNotEmpty.

I am new to Guard API's, so I might be missing some best practices, but noticed this possible shortcoming in the package immediately when starting to use it in our project. Also searched for Stackoverflow and discussions, but there was no discussion to be found around Guid's and Guard.

Help us help you

Yes, I'd like to be assigned to work on this item

AlexRadch commented 8 months ago

@aviita Look at the Guard.IsNotDefault method, I think Guid.Empty == default(Guid) so this method will work as you want.

aviita commented 8 months ago

@aviita Look at the Guard.IsNotDefault method, I think Guid.Empty == default(Guid) so this method will work as you want.

Thanks. Will do.