dotnet
dotnet copied to clipboard
Guard for IsNotEmpty(Guid)
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
@aviita Look at the Guard.IsNotDefault
method, I think Guid.Empty == default(Guid)
so this method will work as you want.
@aviita Look at the
Guard.IsNotDefault
method, I thinkGuid.Empty == default(Guid)
so this method will work as you want.
Thanks. Will do.