Skip to content

Commit

Permalink
Merge pull request #169 from ardalis/errors-as-readonly-collections
Browse files Browse the repository at this point in the history
Expose ValidationErrors as IEnumerable to Prevent Side Effects
  • Loading branch information
KyleMcMaster authored Mar 12, 2024
2 parents 58ad73d + 5435d7e commit c5966e6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 596 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ReturnsInvalidResultGivenEmptyNames()
var result = service.Create("", "");

result.Status.Should().Be(ResultStatus.Invalid);
result.ValidationErrors.Count.Should().Be(2);
result.ValidationErrors.Should().HaveCount(2);
}

[Fact]
Expand All @@ -26,7 +26,7 @@ public void ReturnsInvalidResultWith2ErrorsGivenSomeLongNameSurname()
var result = service.Create("Steve", "SomeLongName");

result.Status.Should().Be(ResultStatus.Invalid);
result.ValidationErrors.Count.Should().Be(2);
result.ValidationErrors.Should().HaveCount(2);
}

[Fact]
Expand Down
7 changes: 3 additions & 4 deletions src/Ardalis.Result/IResult.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

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

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

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

result.Initialize();

return result;
return new Result(ResultStatus.Error) { Errors = errorMessages };
}

/// <summary>
Expand All @@ -79,15 +70,11 @@ public static Result<T> Success<T>(T value, string successMessage)
/// <returns>A Result</returns>
public static Result ErrorWithCorrelationId(string correlationId, params string[] 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;
return new Result(ResultStatus.Error)
{
CorrelationId = correlationId,
Errors = errorMessages
};
}

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

/// <summary>
Expand All @@ -107,24 +94,17 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
/// <returns>A Result</returns>
public new static Result Invalid(params ValidationError[] validationErrors)
{
Result result = new Result(ResultStatus.Invalid);

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

result.Initialize();

return result;
return new Result(ResultStatus.Invalid) { ValidationErrors = new List<ValidationError>(validationErrors) };
}

/// <summary>
/// Represents validation errors that prevent the underlying service from completing.
/// </summary>
/// <param name="validationErrors">A list of validation errors encountered</param>
/// <returns>A Result</returns>
public new static Result Invalid(List<ValidationError> validationErrors)
public new static Result Invalid(IEnumerable<ValidationError> validationErrors)
{
return Invalid(validationErrors.ToArray());
return new Result(ResultStatus.Invalid) { ValidationErrors = validationErrors };
}

/// <summary>
Expand All @@ -133,7 +113,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
/// <returns>A Result</returns>
public new static Result NotFound()
{
return NotFound(null);
return new Result(ResultStatus.NotFound);
}

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

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

result.Initialize();

return result;
return new Result(ResultStatus.NotFound) { Errors = errorMessages };
}

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

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

/// <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 @@ -186,7 +155,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
/// <returns>A Result<typeparamref name="T"/></returns>
public new static Result Conflict()
{
return Conflict(null);
return new Result(ResultStatus.Conflict);
}

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

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

result.Initialize();

return result;
return new Result(ResultStatus.Conflict) { Errors = errorMessages };
}

/// <summary>
Expand All @@ -218,32 +180,18 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
/// <returns></returns>
public new static Result Unavailable(params string[] errorMessages)
{
Result result = new Result(ResultStatus.Unavailable);

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

result.Initialize();

return result;
return new Result(ResultStatus.Unavailable) { Errors = errorMessages };
}

/// 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
/// </summary>
/// <param name="errorMessages">A list of string error messages.</param>
/// <returns>A Result</returns>
public static Result CriticalError(params string[] errorMessages)
public new static Result CriticalError(params string[] errorMessages)
{
Result result = new Result(ResultStatus.CriticalError);

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

result.Initialize();

return result;
return new Result(ResultStatus.CriticalError) { Errors = errorMessages };
}
}
}
Loading

0 comments on commit c5966e6

Please sign in to comment.