Skip to content

Commit

Permalink
closes #161 (#162)
Browse files Browse the repository at this point in the history
Auto-evaluatie the Status property when Errors ou ValidationErrors are added to the result.
It only works when teh result is created as Ok(), Error() ou Invalid()

Co-authored-by: Ewerton Mattos <ewer@outlook.com>
Co-authored-by: Steve Smith <steve@kentsmiths.com>
  • Loading branch information
3 people committed Feb 28, 2024
1 parent 9dd19c4 commit 60a0dbe
Show file tree
Hide file tree
Showing 6 changed files with 589 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/Ardalis.Result/IResult.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Ardalis.Result
{
public interface IResult
{
ResultStatus Status { get; }
IEnumerable<string> Errors { get; }
List<ValidationError> ValidationErrors { get; }
ObservableCollection<string> Errors { get; }
ObservableCollection<ValidationError> ValidationErrors { get; }
Type ValueType { get; }
Object GetValue();
}
Expand Down
90 changes: 71 additions & 19 deletions src/Ardalis.Result/Result.Void.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Ardalis.Result
{
Expand Down Expand Up @@ -57,7 +59,14 @@ public static Result<T> Success<T>(T value, string successMessage)
/// <returns>A Result</returns>
public new static Result Error(params string[] errorMessages)
{
return new Result(ResultStatus.Error) { Errors = errorMessages };
Result result = new Result(ResultStatus.Error);

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}

/// <summary>
Expand All @@ -70,11 +79,15 @@ public new static Result Error(params string[] errorMessages)
/// <returns>A Result</returns>
public static Result ErrorWithCorrelationId(string correlationId, params string[] errorMessages)
{
return new Result(ResultStatus.Error)
{
CorrelationId = correlationId,
Errors = errorMessages
};
Result result = new Result(ResultStatus.Error);
result.CorrelationId = correlationId;

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}

/// <summary>
Expand All @@ -84,7 +97,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
/// <returns>A Result</returns>
public new static Result Invalid(ValidationError validationError)
{
return new Result(ResultStatus.Invalid) { ValidationErrors = { validationError } };
return Invalid(new List<ValidationError> { validationError });
}

/// <summary>
Expand All @@ -94,7 +107,14 @@ public new static Result Invalid(ValidationError validationError)
/// <returns>A Result</returns>
public new static Result Invalid(params ValidationError[] validationErrors)
{
return new Result(ResultStatus.Invalid) { ValidationErrors = new List<ValidationError>(validationErrors) };
Result result = new Result(ResultStatus.Invalid);

if (validationErrors != null && validationErrors.Length > 0)
result.ValidationErrors = new ObservableCollection<ValidationError>(validationErrors);

result.Initialize();

return result;
}

/// <summary>
Expand All @@ -104,7 +124,7 @@ public new static Result Invalid(params ValidationError[] validationErrors)
/// <returns>A Result</returns>
public new static Result Invalid(List<ValidationError> validationErrors)
{
return new Result(ResultStatus.Invalid) { ValidationErrors = validationErrors };
return Invalid(validationErrors.ToArray());
}

/// <summary>
Expand All @@ -113,7 +133,7 @@ public new static Result Invalid(List<ValidationError> validationErrors)
/// <returns>A Result</returns>
public new static Result NotFound()
{
return new Result(ResultStatus.NotFound);
return NotFound(null);
}

/// <summary>
Expand All @@ -124,7 +144,14 @@ public new static Result NotFound()
/// <returns>A Result</returns>
public new static Result NotFound(params string[] errorMessages)
{
return new Result(ResultStatus.NotFound) { Errors = errorMessages };
Result result = new Result(ResultStatus.NotFound);

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}

/// <summary>
Expand All @@ -134,7 +161,9 @@ public new static Result NotFound(params string[] errorMessages)
/// <returns>A Result</returns>
public new static Result Forbidden()
{
return new Result(ResultStatus.Forbidden);
Result result = new Result(ResultStatus.Forbidden);
result.Initialize();
return result;
}

/// <summary>
Expand All @@ -144,9 +173,11 @@ public new static Result Forbidden()
/// <returns>A Result</returns>
public new static Result Unauthorized()
{
return new Result(ResultStatus.Unauthorized);
Result result = new Result(ResultStatus.Unauthorized);
result.Initialize();
return result;
}

/// <summary>
/// Represents a situation where a service is in conflict due to the current state of a resource,
/// such as an edit conflict between multiple concurrent updates.
Expand All @@ -155,7 +186,7 @@ public new static Result Unauthorized()
/// <returns>A Result<typeparamref name="T"/></returns>
public new static Result Conflict()
{
return new Result(ResultStatus.Conflict);
return Conflict(null);
}

/// <summary>
Expand All @@ -168,7 +199,14 @@ public new static Result Conflict()
/// <returns>A Result<typeparamref name="T"/></returns>
public new static Result Conflict(params string[] errorMessages)
{
return new Result(ResultStatus.Conflict) { Errors = errorMessages };
Result result = new Result(ResultStatus.Conflict);

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}

/// <summary>
Expand All @@ -180,9 +218,16 @@ public new static Result Conflict(params string[] errorMessages)
/// <returns></returns>
public new static Result Unavailable(params string[] errorMessages)
{
return new Result(ResultStatus.Unavailable) { Errors = errorMessages };
Result result = new Result(ResultStatus.Unavailable);

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}

/// Represents a critical error that occurred during the execution of the service.
/// Everything provided by the user was valid, but the service was unable to complete due to an exception.
/// See also HTTP 500 Internal Server Error: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors
Expand All @@ -191,7 +236,14 @@ public new static Result Unavailable(params string[] errorMessages)
/// <returns>A Result</returns>
public static Result CriticalError(params string[] errorMessages)

Check warning on line 237 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.CriticalError(params string[])' hides inherited member 'Result<Result>.CriticalError(params string[])'. Use the new keyword if hiding was intended.

Check warning on line 237 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.CriticalError(params string[])' hides inherited member 'Result<Result>.CriticalError(params string[])'. Use the new keyword if hiding was intended.

Check warning on line 237 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.CriticalError(params string[])' hides inherited member 'Result<Result>.CriticalError(params string[])'. Use the new keyword if hiding was intended.
{
return new Result(ResultStatus.CriticalError) { Errors = errorMessages };
Result result = new Result(ResultStatus.CriticalError);

if (errorMessages != null && errorMessages.Length > 0)
result.Errors = new ObservableCollection<string>(errorMessages);

result.Initialize();

return result;
}
}
}
Loading

0 comments on commit 60a0dbe

Please sign in to comment.