Description
Background and Motivation
IdentityResult
provides useful factory Failed
method for creating failed IdentityResult
, however it allocates array due to it accepting params IdentityError[] errors
as argument, even if user passes only 1 error.
My proposal is to add overload to Failed
method that will accept one IdentityError
that will not allocate array in such case.
Proposed API
namespace Microsoft.AspNetCore.Identity;
public class IdentityResult
{
/*
Existing overload
public static IdentityResult Failed(params IdentityError[] errors);
*/
+ public static IdentityResult Failed(IdentityError error);
}
Usage Examples
From user perspective nothing will change, the same method will be called, in the same way, unless they created params
array explicitly;
// current
var result = IdentityResult.Failed();
result = IdentityResult.Failed(new IdentityError() // allocates array
{
Description = "Error description"
});
result = IdentityResult.Failed(new IdentityError()
{
Description = "Error description"
}, new IdentityError()
{
Description = "Error description2"
});
// with proposed API
var result = IdentityResult.Failed();
result = IdentityResult.Failed(new IdentityError() // doesn't allocate array
{
Description = "Error description"
});
result = IdentityResult.Failed(new IdentityError()
{
Description = "Error description"
}, new IdentityError()
{
Description = "Error description2"
});
Alternative Designs
Create overload accepting params Span<IdentityError>
when/if dotnet/csharplang#1757 is implemented.
Risks
We would need to ensure that behavior of new overload is consistent with current overload, since after recompilation compiler will pick overload without params array, for cases when user calls Failed
method with single error passed as argument. And behavior won't change unexpectedly for developer.