diff --git a/ManagedCode.Communication.AspNetCore/WebApi/Extensions/ControllerExtensions.cs b/ManagedCode.Communication.AspNetCore/WebApi/Extensions/ControllerExtensions.cs index 6b22fae..82fdc6a 100644 --- a/ManagedCode.Communication.AspNetCore/WebApi/Extensions/ControllerExtensions.cs +++ b/ManagedCode.Communication.AspNetCore/WebApi/Extensions/ControllerExtensions.cs @@ -10,7 +10,7 @@ public static IActionResult ToActionResult(this Result result) if (result.IsSuccess) return new OkObjectResult(result.Value); - var problem = result.Problem ?? Problem.Create("Operation failed", "Unknown error occurred", 500); + var problem = result.GetProblemNoFallback() ?? Problem.Create("Operation failed", "Unknown error occurred", 500); return new ObjectResult(problem) { StatusCode = problem.StatusCode @@ -22,7 +22,7 @@ public static IActionResult ToActionResult(this Result result) if (result.IsSuccess) return new NoContentResult(); - var problem = result.Problem ?? Problem.Create("Operation failed", "Unknown error occurred", 500); + var problem = result.GetProblemNoFallback() ?? Problem.Create("Operation failed", "Unknown error occurred", 500); return new ObjectResult(problem) { StatusCode = problem.StatusCode @@ -34,7 +34,7 @@ public static Microsoft.AspNetCore.Http.IResult ToHttpResult(this Result r if (result.IsSuccess) return Results.Ok(result.Value); - var problem = result.Problem ?? Problem.Create("Operation failed", "Unknown error occurred", 500); + var problem = result.GetProblemNoFallback() ?? Problem.Create("Operation failed", "Unknown error occurred", 500); return Results.Problem( title: problem.Title, detail: problem.Detail, @@ -50,7 +50,7 @@ public static Microsoft.AspNetCore.Http.IResult ToHttpResult(this Result result) if (result.IsSuccess) return Results.NoContent(); - var problem = result.Problem ?? Problem.Create("Operation failed", "Unknown error occurred", 500); + var problem = result.GetProblemNoFallback() ?? Problem.Create("Operation failed", "Unknown error occurred", 500); return Results.Problem( title: problem.Title, detail: problem.Detail, diff --git a/ManagedCode.Communication.Orleans/Converters/CollectionResultTSurrogateConverter.cs b/ManagedCode.Communication.Orleans/Converters/CollectionResultTSurrogateConverter.cs index f433d4f..f0665cb 100644 --- a/ManagedCode.Communication.Orleans/Converters/CollectionResultTSurrogateConverter.cs +++ b/ManagedCode.Communication.Orleans/Converters/CollectionResultTSurrogateConverter.cs @@ -9,8 +9,10 @@ public sealed class CollectionResultTSurrogateConverter : IConverter ConvertFromSurrogate(in CollectionResultTSurrogate surrogate) { - return CollectionResult.Create(surrogate.IsSuccess, surrogate.Collection, surrogate.PageNumber, surrogate.PageSize, surrogate.TotalItems, - surrogate.Problem); + if (surrogate.IsSuccess) + return CollectionResult.CreateSuccess(surrogate.Collection, surrogate.PageNumber, surrogate.PageSize, surrogate.TotalItems); + + return CollectionResult.CreateFailed(surrogate.Problem ?? Problem.GenericError(), surrogate.Collection); } public CollectionResultTSurrogate ConvertToSurrogate(in CollectionResult value) diff --git a/ManagedCode.Communication.Orleans/Converters/ResultSurrogateConverter.cs b/ManagedCode.Communication.Orleans/Converters/ResultSurrogateConverter.cs index 87886ac..ccb8dd6 100644 --- a/ManagedCode.Communication.Orleans/Converters/ResultSurrogateConverter.cs +++ b/ManagedCode.Communication.Orleans/Converters/ResultSurrogateConverter.cs @@ -8,7 +8,10 @@ public sealed class ResultSurrogateConverter : IConverter : IConverter, ResultT { public Result ConvertFromSurrogate(in ResultTSurrogate surrogate) { - return Result.Create(surrogate.IsSuccess, surrogate.Value, surrogate.Problem); + if (surrogate.IsSuccess) + return Result.Succeed(surrogate.Value!); + + return Result.CreateFailed(surrogate.Problem ?? Problem.GenericError(), surrogate.Value); } public ResultTSurrogate ConvertToSurrogate(in Result value) diff --git a/ManagedCode.Communication.Tests/AspNetCore/Extensions/ControllerExtensionsTests.cs b/ManagedCode.Communication.Tests/AspNetCore/Extensions/ControllerExtensionsTests.cs index 5fd4660..c638868 100644 --- a/ManagedCode.Communication.Tests/AspNetCore/Extensions/ControllerExtensionsTests.cs +++ b/ManagedCode.Communication.Tests/AspNetCore/Extensions/ControllerExtensionsTests.cs @@ -86,7 +86,7 @@ public void ToActionResult_WithValidationError_Returns400WithProblemDetails() public void ToActionResult_WithNoProblem_ReturnsDefaultError() { // Arrange - manually create failed result without problem - var result = new Result { IsSuccess = false, Problem = null }; + var result = (Result)default; // Act var actionResult = result.ToActionResult(); diff --git a/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFailMethodsTests.cs b/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFailMethodsTests.cs index b2d4161..3fac23a 100644 --- a/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFailMethodsTests.cs +++ b/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFailMethodsTests.cs @@ -28,7 +28,7 @@ public void Fail_NoParameters_ShouldCreateFailedResult() result.PageSize.Should().Be(0); result.TotalItems.Should().Be(0); result.TotalPages.Should().Be(0); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion @@ -48,7 +48,7 @@ public void Fail_WithEnumerable_ShouldCreateFailedResultWithItems() result.IsFailed.Should().BeTrue(); result.Collection.Should().BeEquivalentTo(items); result.Collection.Should().HaveCount(5); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } [Fact] @@ -84,7 +84,7 @@ public void Fail_WithArray_ShouldCreateFailedResultWithItems() result.IsFailed.Should().BeTrue(); result.Collection.Should().BeEquivalentTo(items); result.Collection.Should().HaveCount(3); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } [Fact] diff --git a/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFromMethodsTests.cs b/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFromMethodsTests.cs index 74edc16..611fca4 100644 --- a/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFromMethodsTests.cs +++ b/ManagedCode.Communication.Tests/CollectionResults/CollectionResultFromMethodsTests.cs @@ -460,7 +460,7 @@ public void From_FailedCollectionResultWithoutProblem_ShouldReturnFailedResult() // Assert result.IsFailed.Should().BeTrue(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion @@ -507,7 +507,7 @@ public void From_GenericFailedCollectionResultWithoutProblem_ShouldReturnFailedR // Assert result.IsFailed.Should().BeTrue(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion diff --git a/ManagedCode.Communication.Tests/Extensions/ProblemExtensionsTests.cs b/ManagedCode.Communication.Tests/Extensions/ProblemExtensionsTests.cs index 7e14e29..a696bfb 100644 --- a/ManagedCode.Communication.Tests/Extensions/ProblemExtensionsTests.cs +++ b/ManagedCode.Communication.Tests/Extensions/ProblemExtensionsTests.cs @@ -286,4 +286,43 @@ public void ToFailedResult_WithComplexExtensions_ShouldPreserveAllData() errors["email"].Should().Contain("Already exists"); errors["password"].Should().Contain("Too short"); } -} \ No newline at end of file + + [Fact] + public void AddInvalidMessage_ShouldAddValidationError() + { + // Arrange + var problem = new Problem(); + + // Act + problem.AddValidationError("email", "Email is required"); + problem.AddValidationError("email", "Email format is invalid"); + + // Assert + problem.InvalidField("email") + .Should() + .BeTrue(); + var emailErrors = problem.InvalidFieldError("email"); + emailErrors.Should() + .Contain("Email is required"); + emailErrors.Should() + .Contain("Email format is invalid"); + } + + [Fact] + public void AddInvalidMessage_WithGeneralMessage_ShouldAddToGeneralErrors() + { + // Arrange + var problem = new Problem(); + + // Act + problem.AddValidationError("General error occurred"); + + // Assert + problem.InvalidField("_general") + .Should() + .BeTrue(); + var generalErrors = problem.InvalidFieldError("_general"); + generalErrors.Should() + .Be("General error occurred"); + } +} diff --git a/ManagedCode.Communication.Tests/Results/CollectionResultTests.cs b/ManagedCode.Communication.Tests/Results/CollectionResultTests.cs index df08781..e6167c7 100644 --- a/ManagedCode.Communication.Tests/Results/CollectionResultTests.cs +++ b/ManagedCode.Communication.Tests/Results/CollectionResultTests.cs @@ -292,45 +292,6 @@ public void InvalidFieldError_WithValidationProblem_ShouldReturnErrorMessage() .BeEmpty(); } - [Fact] - public void AddInvalidMessage_ShouldAddValidationError() - { - // Arrange - var result = CollectionResult.Empty(); - - // Act - result.AddInvalidMessage("email", "Email is required"); - result.AddInvalidMessage("email", "Email format is invalid"); - - // Assert - result.InvalidField("email") - .Should() - .BeTrue(); - var emailErrors = result.InvalidFieldError("email"); - emailErrors.Should() - .Contain("Email is required"); - emailErrors.Should() - .Contain("Email format is invalid"); - } - - [Fact] - public void AddInvalidMessage_WithGeneralMessage_ShouldAddToGeneralErrors() - { - // Arrange - var result = CollectionResult.Empty(); - - // Act - result.AddInvalidMessage("General error occurred"); - - // Assert - result.InvalidField("_general") - .Should() - .BeTrue(); - var generalErrors = result.InvalidFieldError("_general"); - generalErrors.Should() - .Be("General error occurred"); - } - [Fact] public void ThrowIfFail_WithSuccessfulResult_ShouldNotThrow() { diff --git a/ManagedCode.Communication.Tests/Results/ResultFailMethodsTests.cs b/ManagedCode.Communication.Tests/Results/ResultFailMethodsTests.cs index 3f2f06a..4c315b2 100644 --- a/ManagedCode.Communication.Tests/Results/ResultFailMethodsTests.cs +++ b/ManagedCode.Communication.Tests/Results/ResultFailMethodsTests.cs @@ -20,7 +20,7 @@ public void Result_Fail_NoParameters_ShouldCreateFailedResult() // Assert result.IsFailed.Should().BeTrue(); result.IsSuccess.Should().BeFalse(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion diff --git a/ManagedCode.Communication.Tests/Results/ResultStaticHelperMethodsTests.cs b/ManagedCode.Communication.Tests/Results/ResultStaticHelperMethodsTests.cs index 032e51b..5cc5213 100644 --- a/ManagedCode.Communication.Tests/Results/ResultStaticHelperMethodsTests.cs +++ b/ManagedCode.Communication.Tests/Results/ResultStaticHelperMethodsTests.cs @@ -22,7 +22,7 @@ public void Result_FailT_NoParameters_ShouldCreateFailedResultT() result.IsFailed.Should().BeTrue(); result.IsSuccess.Should().BeFalse(); result.Value.Should().BeNull(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } [Fact] diff --git a/ManagedCode.Communication.Tests/Results/ResultTFailMethodsTests.cs b/ManagedCode.Communication.Tests/Results/ResultTFailMethodsTests.cs index b737f76..196e695 100644 --- a/ManagedCode.Communication.Tests/Results/ResultTFailMethodsTests.cs +++ b/ManagedCode.Communication.Tests/Results/ResultTFailMethodsTests.cs @@ -22,7 +22,7 @@ public void ResultT_Fail_NoParameters_ShouldCreateFailedResult() result.IsFailed.Should().BeTrue(); result.IsSuccess.Should().BeFalse(); result.Value.Should().BeNull(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion @@ -41,7 +41,7 @@ public void ResultT_Fail_WithValue_ShouldCreateFailedResultWithValue() // Assert result.IsFailed.Should().BeTrue(); result.Value.Should().Be(value); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } [Fact] @@ -54,7 +54,7 @@ public void ResultT_Fail_WithNullValue_ShouldCreateFailedResultWithNull() // Assert result.IsFailed.Should().BeTrue(); result.Value.Should().BeNull(); - result.HasProblem.Should().BeFalse(); + result.HasProblem.Should().BeTrue(); } #endregion diff --git a/ManagedCode.Communication/CollectionResultT/CollectionResult.cs b/ManagedCode.Communication/CollectionResultT/CollectionResult.cs index 019d539..d42f3ac 100644 --- a/ManagedCode.Communication/CollectionResultT/CollectionResult.cs +++ b/ManagedCode.Communication/CollectionResultT/CollectionResult.cs @@ -18,7 +18,7 @@ private CollectionResult(bool isSuccess, IEnumerable? collection, int pageNum { } - internal CollectionResult(bool isSuccess, T[]? collection, int pageNumber, int pageSize, int totalItems, Problem? problem = null) + private CollectionResult(bool isSuccess, T[]? collection, int pageNumber, int pageSize, int totalItems, Problem? problem = null) { IsSuccess = isSuccess; Collection = collection ?? []; @@ -29,11 +29,17 @@ internal CollectionResult(bool isSuccess, T[]? collection, int pageNumber, int p Problem = problem; } - internal static CollectionResult Create(bool isSuccess, T[]? collection, int pageNumber, int pageSize, int totalItems, Problem? problem = null) + internal static CollectionResult CreateSuccess(T[]? collection, int pageNumber, int pageSize, int totalItems) { - return new CollectionResult(isSuccess, collection, pageNumber, pageSize, totalItems, problem); + return new CollectionResult(true, collection, pageNumber, pageSize, totalItems, null); } + internal static CollectionResult CreateFailed(Problem problem, T[]? collection = null) + { + return new CollectionResult(false, collection, 0, 0, 0, problem); + } + + [JsonInclude] [JsonPropertyName("isSuccess")] [JsonPropertyOrder(1)] [MemberNotNullWhen(true, nameof(Collection))] @@ -41,7 +47,7 @@ internal static CollectionResult Create(bool isSuccess, T[]? collection, int public bool IsSuccess { get; set; } [JsonIgnore] - public bool IsFailed => !IsSuccess || HasProblem; + public bool IsFailed => !IsSuccess; [JsonPropertyName("collection")] [JsonPropertyOrder(2)] @@ -64,10 +70,24 @@ internal static CollectionResult Create(bool isSuccess, T[]? collection, int [JsonPropertyOrder(6)] public int TotalPages { get; set; } + [JsonInclude] [JsonPropertyName("problem")] [JsonPropertyOrder(7)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public Problem? Problem { get; set; } + private Problem? _problem; + + [JsonIgnore] + public Problem? Problem + { + get + { + if (_problem is null && !IsSuccess) + _problem = Problem.GenericError(); + + return _problem; + } + private init => _problem = value; + } [JsonIgnore] public bool IsEmpty => Collection is null || Collection.Length == 0; @@ -77,15 +97,22 @@ internal static CollectionResult Create(bool isSuccess, T[]? collection, int [JsonIgnore] [MemberNotNullWhen(true, nameof(Problem))] - public bool HasProblem => Problem != null; + public bool HasProblem => !IsSuccess; #region IResultProblem Implementation + /// + /// Get the Problem assigned to the result without falling back to a generic error if no problem is assigned. + /// Useful if a different default problem is desired. + /// + internal Problem? GetProblemNoFallback() => _problem; + public bool ThrowIfFail() { - if (HasProblem) + var problem = Problem; + if (problem is not null) { - throw Problem; + throw problem; } return false; @@ -95,7 +122,7 @@ public bool ThrowIfFail() public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) { problem = Problem; - return HasProblem; + return problem is not null; } #endregion @@ -108,57 +135,28 @@ public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) public bool InvalidField(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.ContainsKey(fieldName) ?? false; + return !IsSuccess && Problem.InvalidField(fieldName); } public string InvalidFieldError(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.TryGetValue(fieldName, out var fieldErrors) == true ? string.Join(", ", fieldErrors) : string.Empty; + return IsSuccess + ? string.Empty + : Problem.InvalidFieldError(fieldName); } + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string message) { - if (Problem == null) - { - Problem = Problem.Validation((ProblemConstants.ValidationFields.General, message)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(ProblemConstants.ValidationFields.General)) - { - errors[ProblemConstants.ValidationFields.General] = new List(); - } - - errors[ProblemConstants.ValidationFields.General] - .Add(message); - } - } + if (!IsSuccess) + Problem.AddValidationError(message); } + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string key, string value) { - if (Problem == null) - { - Problem = Problem.Validation((key, value)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(key)) - { - errors[key] = new List(); - } - - errors[key].Add(value); - } - } + if (!IsSuccess) + Problem.AddValidationError(key, value); } #endregion @@ -170,7 +168,7 @@ public void AddInvalidMessage(string key, string value) /// public static CollectionResult Empty() { - return Create(true, [], 0, 0, 0); + return CreateSuccess([], 0, 0, 0); } #endregion diff --git a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs index 0a208b3..8b725b9 100644 --- a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs +++ b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs @@ -10,55 +10,55 @@ public partial struct CollectionResult { public static CollectionResult Fail() { - return Create(false, default, 0, 0, 0); + return CreateFailed(Problem.GenericError()); } public static CollectionResult Fail(IEnumerable value) { - return Create(false, value.ToArray(), 0, 0, 0); + return CreateFailed(Problem.GenericError(), value.ToArray()); } public static CollectionResult Fail(T[] value) { - return Create(false, value, 0, 0, 0); + return CreateFailed(Problem.GenericError(), value); } public static CollectionResult Fail(Problem problem) { - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult Fail(string title) { var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult Fail(string title, string detail) { var problem = Problem.Create(title, detail); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult Fail(string title, string detail, HttpStatusCode status) { var problem = Problem.Create(title, detail, (int)status); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult Fail(Exception exception) { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); + return CreateFailed(Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } public static CollectionResult Fail(Exception exception, HttpStatusCode status) { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(exception, (int)status)); + return CreateFailed(Problem.Create(exception, (int)status)); } public static CollectionResult FailValidation(params (string field, string message)[] errors) { - return new CollectionResult(false, default, 0, 0, 0, Problem.Validation(errors)); + return CreateFailed(Problem.Validation(errors)); } public static CollectionResult FailBadRequest() @@ -68,7 +68,7 @@ public static CollectionResult FailBadRequest() ProblemConstants.Messages.BadRequest, (int)HttpStatusCode.BadRequest); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailBadRequest(string detail) @@ -78,7 +78,7 @@ public static CollectionResult FailBadRequest(string detail) detail, (int)HttpStatusCode.BadRequest); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailUnauthorized() @@ -88,7 +88,7 @@ public static CollectionResult FailUnauthorized() ProblemConstants.Messages.UnauthorizedAccess, (int)HttpStatusCode.Unauthorized); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailUnauthorized(string detail) @@ -98,7 +98,7 @@ public static CollectionResult FailUnauthorized(string detail) detail, (int)HttpStatusCode.Unauthorized); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailForbidden() @@ -108,7 +108,7 @@ public static CollectionResult FailForbidden() ProblemConstants.Messages.ForbiddenAccess, (int)HttpStatusCode.Forbidden); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailForbidden(string detail) @@ -118,7 +118,7 @@ public static CollectionResult FailForbidden(string detail) detail, (int)HttpStatusCode.Forbidden); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailNotFound() @@ -128,7 +128,7 @@ public static CollectionResult FailNotFound() ProblemConstants.Messages.ResourceNotFound, (int)HttpStatusCode.NotFound); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult FailNotFound(string detail) @@ -138,26 +138,26 @@ public static CollectionResult FailNotFound(string detail) detail, (int)HttpStatusCode.NotFound); - return Create(false, default, 0, 0, 0, problem); + return CreateFailed(problem); } public static CollectionResult Fail(TEnum errorCode) where TEnum : Enum { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode)); + return CreateFailed(Problem.Create(errorCode)); } public static CollectionResult Fail(TEnum errorCode, string detail) where TEnum : Enum { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, detail)); + return CreateFailed(Problem.Create(errorCode, detail)); } public static CollectionResult Fail(TEnum errorCode, HttpStatusCode status) where TEnum : Enum { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, errorCode.ToString(), (int)status)); + return CreateFailed(Problem.Create(errorCode, errorCode.ToString(), (int)status)); } public static CollectionResult Fail(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum { - return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, detail, (int)status)); + return CreateFailed(Problem.Create(errorCode, detail, (int)status)); } } diff --git a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Succeed.cs b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Succeed.cs index d9d9def..d126be8 100644 --- a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Succeed.cs +++ b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Succeed.cs @@ -7,22 +7,22 @@ public partial struct CollectionResult { public static CollectionResult Succeed(T[] value, int pageNumber, int pageSize, int totalItems) { - return new CollectionResult(true, value, pageNumber, pageSize, totalItems); + return CreateSuccess(value, pageNumber, pageSize, totalItems); } public static CollectionResult Succeed(IEnumerable value, int pageNumber, int pageSize, int totalItems) { - return new CollectionResult(true, value.ToArray(), pageNumber, pageSize, totalItems); + return CreateSuccess(value.ToArray(), pageNumber, pageSize, totalItems); } public static CollectionResult Succeed(T[] value) { - return new CollectionResult(true, value, 1, value.Length, value.Length); + return CreateSuccess(value, 1, value.Length, value.Length); } public static CollectionResult Succeed(IEnumerable value) { var array = value.ToArray(); - return new CollectionResult(true, array, 1, array.Length, array.Length); + return CreateSuccess(array, 1, array.Length, array.Length); } -} \ No newline at end of file +} diff --git a/ManagedCode.Communication/IResultInvalid.cs b/ManagedCode.Communication/IResultInvalid.cs index 5d4e6d9..c164e2f 100644 --- a/ManagedCode.Communication/IResultInvalid.cs +++ b/ManagedCode.Communication/IResultInvalid.cs @@ -1,3 +1,5 @@ +using System; + namespace ManagedCode.Communication; /// @@ -15,6 +17,7 @@ public interface IResultInvalid /// Adds an invalid message to the result. /// /// The invalid message to add. + [Obsolete("Use Problem.AddValidationError instead")] void AddInvalidMessage(string message); /// @@ -22,5 +25,6 @@ public interface IResultInvalid /// /// The key of the invalid message. /// The value of the invalid message. + [Obsolete("Use Problem.AddValidationError instead")] void AddInvalidMessage(string key, string value); -} \ No newline at end of file +} diff --git a/ManagedCode.Communication/IResultProblem.cs b/ManagedCode.Communication/IResultProblem.cs index c003b73..30def8b 100644 --- a/ManagedCode.Communication/IResultProblem.cs +++ b/ManagedCode.Communication/IResultProblem.cs @@ -8,9 +8,9 @@ namespace ManagedCode.Communication; public interface IResultProblem { /// - /// Gets or sets the problem details. + /// Gets the problem details. /// - Problem? Problem { get; set; } + Problem? Problem { get; } /// /// Determines whether the result has a problem. diff --git a/ManagedCode.Communication/ManagedCode.Communication.csproj b/ManagedCode.Communication/ManagedCode.Communication.csproj index b74939b..6bd43d4 100644 --- a/ManagedCode.Communication/ManagedCode.Communication.csproj +++ b/ManagedCode.Communication/ManagedCode.Communication.csproj @@ -17,6 +17,7 @@ + diff --git a/ManagedCode.Communication/Problem/Problem.Extensions.cs b/ManagedCode.Communication/Problem/Problem.Extensions.cs index f268380..5b3a1ea 100644 --- a/ManagedCode.Communication/Problem/Problem.Extensions.cs +++ b/ManagedCode.Communication/Problem/Problem.Extensions.cs @@ -470,7 +470,13 @@ public void AddValidationError(string field, string message) fieldErrors.Add(message); } - + + public void AddValidationError(string message) + { + const string field = ProblemConstants.ValidationFields.General; + AddValidationError(field, message); + } + /// /// Gets or creates validation errors dictionary. /// @@ -486,6 +492,18 @@ private Dictionary> GetOrCreateValidationErrors() return errors; } + public bool InvalidField(string fieldName) + { + var errors = GetValidationErrors(); + return errors?.ContainsKey(fieldName) ?? false; + } + + public string InvalidFieldError(string fieldName) + { + var errors = GetValidationErrors(); + return errors?.TryGetValue(fieldName, out var fieldErrors) == true ? string.Join(", ", fieldErrors) : string.Empty; + } + /// /// Creates a copy of this Problem with the specified extensions added. /// diff --git a/ManagedCode.Communication/Result/Result.Fail.cs b/ManagedCode.Communication/Result/Result.Fail.cs index db9cdd6..dce6930 100644 --- a/ManagedCode.Communication/Result/Result.Fail.cs +++ b/ManagedCode.Communication/Result/Result.Fail.cs @@ -11,7 +11,7 @@ public partial struct Result /// public static Result Fail() { - return Create(false); + return CreateFailed(Problem.GenericError()); } /// @@ -19,7 +19,7 @@ public static Result Fail() /// public static Result Fail(Problem problem) { - return Create(false, problem); + return CreateFailed(problem); } @@ -29,7 +29,7 @@ public static Result Fail(Problem problem) public static Result Fail(string title) { var problem = Problem.Create(title, title, HttpStatusCode.InternalServerError); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -38,7 +38,7 @@ public static Result Fail(string title) public static Result Fail(string title, string detail) { var problem = Problem.Create(title, detail, HttpStatusCode.InternalServerError); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -47,7 +47,7 @@ public static Result Fail(string title, string detail) public static Result Fail(string title, string detail, HttpStatusCode status) { var problem = Problem.Create(title, detail, (int)status); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -55,7 +55,7 @@ public static Result Fail(string title, string detail, HttpStatusCode status) /// public static Result Fail(Exception exception) { - return Create(false, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); + return CreateFailed(Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } /// @@ -63,7 +63,7 @@ public static Result Fail(Exception exception) /// public static Result Fail(Exception exception, HttpStatusCode status) { - return Create(false, Problem.Create(exception, (int)status)); + return CreateFailed(Problem.Create(exception, (int)status)); } /// @@ -71,7 +71,7 @@ public static Result Fail(Exception exception, HttpStatusCode status) /// public static Result FailValidation(params (string field, string message)[] errors) { - return new Result(false, Problem.Validation(errors)); + return CreateFailed(Problem.Validation(errors)); } /// @@ -84,7 +84,7 @@ public static Result FailBadRequest() ProblemConstants.Messages.BadRequest, (int)HttpStatusCode.BadRequest); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -97,7 +97,7 @@ public static Result FailBadRequest(string detail) detail, (int)HttpStatusCode.BadRequest); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -110,7 +110,7 @@ public static Result FailUnauthorized() ProblemConstants.Messages.UnauthorizedAccess, (int)HttpStatusCode.Unauthorized); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -123,7 +123,7 @@ public static Result FailUnauthorized(string detail) detail, (int)HttpStatusCode.Unauthorized); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -136,7 +136,7 @@ public static Result FailForbidden() ProblemConstants.Messages.ForbiddenAccess, (int)HttpStatusCode.Forbidden); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -149,7 +149,7 @@ public static Result FailForbidden(string detail) detail, (int)HttpStatusCode.Forbidden); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -162,7 +162,7 @@ public static Result FailNotFound() ProblemConstants.Messages.ResourceNotFound, (int)HttpStatusCode.NotFound); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -175,7 +175,7 @@ public static Result FailNotFound(string detail) detail, (int)HttpStatusCode.NotFound); - return Create(false, problem); + return CreateFailed(problem); } /// @@ -183,7 +183,7 @@ public static Result FailNotFound(string detail) /// public static Result Fail(TEnum errorCode) where TEnum : Enum { - return Create(false, Problem.Create(errorCode)); + return CreateFailed(Problem.Create(errorCode)); } /// @@ -191,7 +191,7 @@ public static Result Fail(TEnum errorCode) where TEnum : Enum /// public static Result Fail(TEnum errorCode, string detail) where TEnum : Enum { - return Create(false, Problem.Create(errorCode, detail)); + return CreateFailed(Problem.Create(errorCode, detail)); } /// @@ -199,14 +199,14 @@ public static Result Fail(TEnum errorCode, string detail) where TEnum : E /// public static Result Fail(TEnum errorCode, HttpStatusCode status) where TEnum : Enum { - return Create(false, Problem.Create(errorCode, errorCode.ToString(), (int)status)); + return CreateFailed(Problem.Create(errorCode, errorCode.ToString(), (int)status)); } /// /// Creates a failed result from a custom error enum with detail and specific HTTP status. - /// і + /// public static Result Fail(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum { - return Create(false, Problem.Create(errorCode, detail, (int)status)); + return CreateFailed(Problem.Create(errorCode, detail, (int)status)); } } diff --git a/ManagedCode.Communication/Result/Result.Invalid.cs b/ManagedCode.Communication/Result/Result.Invalid.cs index d893734..d55d818 100644 --- a/ManagedCode.Communication/Result/Result.Invalid.cs +++ b/ManagedCode.Communication/Result/Result.Invalid.cs @@ -15,7 +15,7 @@ public static Result Invalid(TEnum code) where TEnum : Enum { var problem = Problem.Validation(("message", nameof(Invalid))); problem.ErrorCode = code.ToString(); - return Create(false, problem); + return CreateFailed(problem); } public static Result Invalid(string message) @@ -27,7 +27,7 @@ public static Result Invalid(TEnum code, string message) where TEnum : En { var problem = Problem.Validation((nameof(message), message)); problem.ErrorCode = code.ToString(); - return Create(false, problem); + return CreateFailed(problem); } public static Result Invalid(string key, string value) @@ -39,7 +39,7 @@ public static Result Invalid(TEnum code, string key, string value) where { var problem = Problem.Validation((key, value)); problem.ErrorCode = code.ToString(); - return Create(false, problem); + return CreateFailed(problem); } public static Result Invalid(Dictionary values) @@ -53,7 +53,7 @@ public static Result Invalid(TEnum code, Dictionary value var problem = Problem.Validation(values.Select(kvp => (kvp.Key, kvp.Value)) .ToArray()); problem.ErrorCode = code.ToString(); - return Create(false, problem); + return CreateFailed(problem); } diff --git a/ManagedCode.Communication/Result/Result.Succeed.cs b/ManagedCode.Communication/Result/Result.Succeed.cs index 7125594..f4ddac8 100644 --- a/ManagedCode.Communication/Result/Result.Succeed.cs +++ b/ManagedCode.Communication/Result/Result.Succeed.cs @@ -6,7 +6,7 @@ public partial struct Result { public static Result Succeed() { - return Create(true); + return CreateSuccess(); } public static Result Succeed(T value) diff --git a/ManagedCode.Communication/Result/Result.cs b/ManagedCode.Communication/Result/Result.cs index 2f7e37d..d67348b 100644 --- a/ManagedCode.Communication/Result/Result.cs +++ b/ManagedCode.Communication/Result/Result.cs @@ -18,41 +18,64 @@ public partial struct Result : IResult /// /// Initializes a new instance of the struct. /// - internal Result(bool isSuccess, Problem? problem = null) + private Result(bool isSuccess, Problem? problem = null) { IsSuccess = isSuccess; Problem = problem; } /// - /// Creates a Result with the specified success status and optional problem. + /// Creates a successful Result. /// - internal static Result Create(bool isSuccess, Problem? problem = null) + internal static Result CreateSuccess() { - return new Result(isSuccess, problem); + return new Result(true, null); + } + + /// + /// Creates a failed Result with the specified problem. + /// + internal static Result CreateFailed(Problem problem) + { + return new Result(false, problem); } /// /// Gets or sets a value indicating whether the operation was successful. /// + [JsonInclude] [JsonPropertyName("isSuccess")] [JsonPropertyOrder(1)] [MemberNotNullWhen(false, nameof(Problem))] - public bool IsSuccess { get; init; } + public bool IsSuccess { get; private init; } /// /// Gets a value indicating whether the operation failed. /// [JsonIgnore] - public bool IsFailed => !IsSuccess || HasProblem; + public bool IsFailed => !IsSuccess; - /// - /// Gets or sets the problem that occurred during the operation. - /// + [JsonInclude] [JsonPropertyName("problem")] [JsonPropertyOrder(2)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public Problem? Problem { get; set; } + private Problem? _problem; + + /// + /// Gets or sets the problem that occurred during the operation. + /// + [JsonIgnore] + public Problem? Problem + { + get + { + if (_problem is null && !IsSuccess) + _problem = Problem.GenericError(); + + return _problem; + } + private init => _problem = value; + } /// @@ -60,17 +83,24 @@ internal static Result Create(bool isSuccess, Problem? problem = null) /// [JsonIgnore] [MemberNotNullWhen(true, nameof(Problem))] - public bool HasProblem => Problem != null; + public bool HasProblem => !IsSuccess; + /// + /// Get the Problem assigned to the result without falling back to a generic error if no problem is assigned. + /// Useful if a different default problem is desired. + /// + internal Problem? GetProblemNoFallback() => _problem; + /// /// Throws an exception if the result indicates a failure. /// public bool ThrowIfFail() { - if (HasProblem) + var problem = Problem; + if (problem is not null) { - throw Problem; + throw problem; } return false; @@ -85,7 +115,7 @@ public bool ThrowIfFail() public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) { problem = Problem; - return HasProblem; + return problem is not null; } @@ -97,58 +127,28 @@ public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) public bool InvalidField(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.ContainsKey(fieldName) ?? false; + return !IsSuccess && Problem.InvalidField(fieldName); } public string InvalidFieldError(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.TryGetValue(fieldName, out var fieldErrors) == true ? string.Join(", ", fieldErrors) : string.Empty; + return IsSuccess + ? string.Empty + : Problem.InvalidFieldError(fieldName); } + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string message) { - if (Problem == null) - { - Problem = Problem.Validation((ProblemConstants.ValidationFields.General, message)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(ProblemConstants.ValidationFields.General)) - { - errors[ProblemConstants.ValidationFields.General] = new List(); - } - - errors[ProblemConstants.ValidationFields.General] - .Add(message); - } - } + if (!IsSuccess) + Problem.AddValidationError(message); } + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string key, string value) { - if (Problem == null) - { - Problem = Problem.Validation((key, value)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(key)) - { - errors[key] = new List(); - } - - errors[key] - .Add(value); - } - } + if (!IsSuccess) + Problem.AddValidationError(key, value); } #endregion diff --git a/ManagedCode.Communication/ResultT/Result.cs b/ManagedCode.Communication/ResultT/Result.cs index 172fd1c..174568c 100644 --- a/ManagedCode.Communication/ResultT/Result.cs +++ b/ManagedCode.Communication/ResultT/Result.cs @@ -19,7 +19,7 @@ public partial struct Result : IResult /// /// Initializes a new instance of the Result struct. /// - internal Result(bool isSuccess, T? value, Problem? problem = null) + private Result(bool isSuccess, T? value, Problem? problem = null) { IsSuccess = isSuccess; Value = value; @@ -31,14 +31,23 @@ internal Result(bool isSuccess, T? value, Problem? problem = null) /// private Result(Exception exception) : this(false, default, Problem.Create(exception)) { + + } + + /// + /// Creates a successful Result with the specified value. + /// + internal static Result CreateSuccess(T value) + { + return new Result(true, value, null); } /// - /// Creates a Result with the specified success status, value and optional problem. + /// Creates a failed Result with the specified problem and optional value. /// - internal static Result Create(bool isSuccess, T? value, Problem? problem = null) + internal static Result CreateFailed(Problem problem, T? value = default) { - return new Result(isSuccess, value, problem); + return new Result(false, value, problem); } @@ -47,9 +56,10 @@ internal static Result Create(bool isSuccess, T? value, Problem? problem = nu /// public bool ThrowIfFail() { - if (HasProblem) + var problem = Problem; + if (problem is not null) { - throw Problem; + throw problem; } return false; @@ -64,16 +74,17 @@ public bool ThrowIfFail() public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) { problem = Problem; - return HasProblem; + return problem is not null; } /// /// Gets a value indicating whether the result is a success. /// + [JsonInclude] [MemberNotNullWhen(true, nameof(Value))] [MemberNotNullWhen(false, nameof(Problem))] - public bool IsSuccess { get; init; } + public bool IsSuccess { get; private init; } /// /// Gets a value indicating whether the result is empty. @@ -86,7 +97,7 @@ public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) /// [JsonIgnore] [MemberNotNullWhen(false, nameof(Value))] - public bool IsFailed => !IsSuccess || HasProblem; + public bool IsFailed => !IsSuccess; /// /// Gets or sets the value of the result. @@ -94,21 +105,40 @@ public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public T? Value { get; set; } + [JsonInclude] + [JsonPropertyName("problem")] + [JsonPropertyOrder(3)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + private Problem? _problem; /// /// Gets or sets the problem that occurred during the operation. /// - [JsonPropertyName("problem")] - [JsonPropertyOrder(3)] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public Problem? Problem { get; set; } + [JsonIgnore] + public Problem? Problem + { + get + { + if (_problem is null && !IsSuccess) + _problem = Problem.GenericError(); + + return _problem; + } + private init => _problem = value; + } /// /// Gets a value indicating whether the result has a problem. /// [JsonIgnore] [MemberNotNullWhen(true, nameof(Problem))] - public bool HasProblem => Problem is not null; + public bool HasProblem => !IsSuccess; + + /// + /// Get the Problem assigned to the result without falling back to a generic error if no problem is assigned. + /// Useful if a different default problem is desired. + /// + internal Problem? GetProblemNoFallback() => _problem; /// /// Gets a value indicating whether the result is invalid. @@ -123,63 +153,33 @@ public bool TryGetProblem([MaybeNullWhen(false)] out Problem problem) /// /// Adds an invalid message to the result. /// + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string message) { - if (Problem == null) - { - Problem = Problem.Validation((ProblemConstants.ValidationFields.General, message)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(ProblemConstants.ValidationFields.General)) - { - errors[ProblemConstants.ValidationFields.General] = new List(); - } - - errors[ProblemConstants.ValidationFields.General] - .Add(message); - } - } + if (!IsSuccess) + Problem.AddValidationError(message); } /// /// Adds an invalid message with a specific key to the result. /// + [Obsolete("Use Problem.AddValidationError instead")] public void AddInvalidMessage(string key, string value) { - if (Problem == null) - { - Problem = Problem.Validation((key, value)); - } - else - { - Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] ??= new Dictionary>(); - if (Problem.Extensions[ProblemConstants.ExtensionKeys.Errors] is Dictionary> errors) - { - if (!errors.ContainsKey(key)) - { - errors[key] = new List(); - } - - errors[key] - .Add(value); - } - } + if (!IsSuccess) + Problem.AddValidationError(key, value); } public bool InvalidField(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.ContainsKey(fieldName) ?? false; + return !IsSuccess && Problem.InvalidField(fieldName); } public string InvalidFieldError(string fieldName) { - var errors = Problem?.GetValidationErrors(); - return errors?.TryGetValue(fieldName, out var fieldErrors) == true ? string.Join(", ", fieldErrors) : string.Empty; + return IsSuccess + ? string.Empty + : Problem.InvalidFieldError(fieldName); } public Dictionary>? InvalidObject => Problem?.GetValidationErrors(); diff --git a/ManagedCode.Communication/ResultT/ResultT.Fail.cs b/ManagedCode.Communication/ResultT/ResultT.Fail.cs index 76ca4bf..cd60aca 100644 --- a/ManagedCode.Communication/ResultT/ResultT.Fail.cs +++ b/ManagedCode.Communication/ResultT/ResultT.Fail.cs @@ -15,7 +15,7 @@ public partial struct Result /// public static Result Fail() { - return Create(false, default); + return CreateFailed(Problem.GenericError()); } /// @@ -23,7 +23,7 @@ public static Result Fail() /// public static Result Fail(T value) { - return Create(false, value); + return CreateFailed(Problem.GenericError(), value); } /// @@ -31,7 +31,7 @@ public static Result Fail(T value) /// public static Result Fail(Problem problem) { - return Create(false, default, problem); + return CreateFailed(problem); } @@ -41,7 +41,7 @@ public static Result Fail(Problem problem) public static Result Fail(string title) { var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -50,7 +50,7 @@ public static Result Fail(string title) public static Result Fail(string title, string detail) { var problem = Problem.Create(title, detail); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -59,7 +59,7 @@ public static Result Fail(string title, string detail) public static Result Fail(string title, string detail, HttpStatusCode status) { var problem = Problem.Create(title, detail, (int)status); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -67,7 +67,7 @@ public static Result Fail(string title, string detail, HttpStatusCode status) /// public static Result Fail(Exception exception) { - return new Result(false, default, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); + return CreateFailed(Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } /// @@ -75,7 +75,7 @@ public static Result Fail(Exception exception) /// public static Result Fail(Exception exception, HttpStatusCode status) { - return new Result(false, default, Problem.Create(exception, (int)status)); + return CreateFailed(Problem.Create(exception, (int)status)); } /// @@ -83,7 +83,7 @@ public static Result Fail(Exception exception, HttpStatusCode status) /// public static Result FailValidation(params (string field, string message)[] errors) { - return new Result(false, default, Problem.Validation(errors)); + return CreateFailed(Problem.Validation(errors)); } /// @@ -96,7 +96,7 @@ public static Result FailBadRequest() ProblemConstants.Messages.BadRequest, (int)HttpStatusCode.BadRequest); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -109,7 +109,7 @@ public static Result FailBadRequest(string detail) detail, (int)HttpStatusCode.BadRequest); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -122,7 +122,7 @@ public static Result FailUnauthorized() ProblemConstants.Messages.UnauthorizedAccess, (int)HttpStatusCode.Unauthorized); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -135,7 +135,7 @@ public static Result FailUnauthorized(string detail) detail, (int)HttpStatusCode.Unauthorized); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -148,7 +148,7 @@ public static Result FailForbidden() ProblemConstants.Messages.ForbiddenAccess, (int)HttpStatusCode.Forbidden); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -161,7 +161,7 @@ public static Result FailForbidden(string detail) detail, (int)HttpStatusCode.Forbidden); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -174,7 +174,7 @@ public static Result FailNotFound() ProblemConstants.Messages.ResourceNotFound, (int)HttpStatusCode.NotFound); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -187,7 +187,7 @@ public static Result FailNotFound(string detail) detail, (int)HttpStatusCode.NotFound); - return Create(false, default, problem); + return CreateFailed(problem); } /// @@ -195,7 +195,7 @@ public static Result FailNotFound(string detail) /// public static Result Fail(TEnum errorCode) where TEnum : Enum { - return new Result(false, default, Problem.Create(errorCode)); + return CreateFailed(Problem.Create(errorCode)); } /// @@ -203,7 +203,7 @@ public static Result Fail(TEnum errorCode) where TEnum : Enum /// public static Result Fail(TEnum errorCode, string detail) where TEnum : Enum { - return new Result(false, default, Problem.Create(errorCode, detail)); + return CreateFailed(Problem.Create(errorCode, detail)); } /// @@ -211,7 +211,7 @@ public static Result Fail(TEnum errorCode, string detail) where TEnum /// public static Result Fail(TEnum errorCode, HttpStatusCode status) where TEnum : Enum { - return new Result(false, default, Problem.Create(errorCode, errorCode.ToString(), (int)status)); + return CreateFailed(Problem.Create(errorCode, errorCode.ToString(), (int)status)); } /// @@ -219,6 +219,6 @@ public static Result Fail(TEnum errorCode, HttpStatusCode status) wher /// public static Result Fail(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum { - return new Result(false, default, Problem.Create(errorCode, detail, (int)status)); + return CreateFailed(Problem.Create(errorCode, detail, (int)status)); } } diff --git a/ManagedCode.Communication/ResultT/ResultT.Succeed.cs b/ManagedCode.Communication/ResultT/ResultT.Succeed.cs index 1189356..a85308b 100644 --- a/ManagedCode.Communication/ResultT/ResultT.Succeed.cs +++ b/ManagedCode.Communication/ResultT/ResultT.Succeed.cs @@ -6,7 +6,7 @@ public partial struct Result { public static Result Succeed(T value) { - return Create(true, value); + return CreateSuccess(value); } public static Result Succeed(Action action)