Skip to content

Commit 60a0dbe

Browse files
Ewertonardalis
andauthored
closes #161 (#162)
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>
1 parent 9dd19c4 commit 60a0dbe

6 files changed

Lines changed: 589 additions & 35 deletions

File tree

src/Ardalis.Result/IResult.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34

45
namespace Ardalis.Result
56
{
67
public interface IResult
78
{
89
ResultStatus Status { get; }
9-
IEnumerable<string> Errors { get; }
10-
List<ValidationError> ValidationErrors { get; }
10+
ObservableCollection<string> Errors { get; }
11+
ObservableCollection<ValidationError> ValidationErrors { get; }
1112
Type ValueType { get; }
1213
Object GetValue();
1314
}

src/Ardalis.Result/Result.Void.cs

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
using System.Linq;
24

35
namespace Ardalis.Result
46
{
@@ -57,7 +59,14 @@ public static Result<T> Success<T>(T value, string successMessage)
5759
/// <returns>A Result</returns>
5860
public new static Result Error(params string[] errorMessages)
5961
{
60-
return new Result(ResultStatus.Error) { Errors = errorMessages };
62+
Result result = new Result(ResultStatus.Error);
63+
64+
if (errorMessages != null && errorMessages.Length > 0)
65+
result.Errors = new ObservableCollection<string>(errorMessages);
66+
67+
result.Initialize();
68+
69+
return result;
6170
}
6271

6372
/// <summary>
@@ -70,11 +79,15 @@ public static Result<T> Success<T>(T value, string successMessage)
7079
/// <returns>A Result</returns>
7180
public static Result ErrorWithCorrelationId(string correlationId, params string[] errorMessages)
7281
{
73-
return new Result(ResultStatus.Error)
74-
{
75-
CorrelationId = correlationId,
76-
Errors = errorMessages
77-
};
82+
Result result = new Result(ResultStatus.Error);
83+
result.CorrelationId = correlationId;
84+
85+
if (errorMessages != null && errorMessages.Length > 0)
86+
result.Errors = new ObservableCollection<string>(errorMessages);
87+
88+
result.Initialize();
89+
90+
return result;
7891
}
7992

8093
/// <summary>
@@ -84,7 +97,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
8497
/// <returns>A Result</returns>
8598
public new static Result Invalid(ValidationError validationError)
8699
{
87-
return new Result(ResultStatus.Invalid) { ValidationErrors = { validationError } };
100+
return Invalid(new List<ValidationError> { validationError });
88101
}
89102

90103
/// <summary>
@@ -94,7 +107,14 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
94107
/// <returns>A Result</returns>
95108
public new static Result Invalid(params ValidationError[] validationErrors)
96109
{
97-
return new Result(ResultStatus.Invalid) { ValidationErrors = new List<ValidationError>(validationErrors) };
110+
Result result = new Result(ResultStatus.Invalid);
111+
112+
if (validationErrors != null && validationErrors.Length > 0)
113+
result.ValidationErrors = new ObservableCollection<ValidationError>(validationErrors);
114+
115+
result.Initialize();
116+
117+
return result;
98118
}
99119

100120
/// <summary>
@@ -104,7 +124,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
104124
/// <returns>A Result</returns>
105125
public new static Result Invalid(List<ValidationError> validationErrors)
106126
{
107-
return new Result(ResultStatus.Invalid) { ValidationErrors = validationErrors };
127+
return Invalid(validationErrors.ToArray());
108128
}
109129

110130
/// <summary>
@@ -113,7 +133,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
113133
/// <returns>A Result</returns>
114134
public new static Result NotFound()
115135
{
116-
return new Result(ResultStatus.NotFound);
136+
return NotFound(null);
117137
}
118138

119139
/// <summary>
@@ -124,7 +144,14 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
124144
/// <returns>A Result</returns>
125145
public new static Result NotFound(params string[] errorMessages)
126146
{
127-
return new Result(ResultStatus.NotFound) { Errors = errorMessages };
147+
Result result = new Result(ResultStatus.NotFound);
148+
149+
if (errorMessages != null && errorMessages.Length > 0)
150+
result.Errors = new ObservableCollection<string>(errorMessages);
151+
152+
result.Initialize();
153+
154+
return result;
128155
}
129156

130157
/// <summary>
@@ -134,7 +161,9 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
134161
/// <returns>A Result</returns>
135162
public new static Result Forbidden()
136163
{
137-
return new Result(ResultStatus.Forbidden);
164+
Result result = new Result(ResultStatus.Forbidden);
165+
result.Initialize();
166+
return result;
138167
}
139168

140169
/// <summary>
@@ -144,9 +173,11 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
144173
/// <returns>A Result</returns>
145174
public new static Result Unauthorized()
146175
{
147-
return new Result(ResultStatus.Unauthorized);
176+
Result result = new Result(ResultStatus.Unauthorized);
177+
result.Initialize();
178+
return result;
148179
}
149-
180+
150181
/// <summary>
151182
/// Represents a situation where a service is in conflict due to the current state of a resource,
152183
/// such as an edit conflict between multiple concurrent updates.
@@ -155,7 +186,7 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
155186
/// <returns>A Result<typeparamref name="T"/></returns>
156187
public new static Result Conflict()
157188
{
158-
return new Result(ResultStatus.Conflict);
189+
return Conflict(null);
159190
}
160191

161192
/// <summary>
@@ -168,7 +199,14 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
168199
/// <returns>A Result<typeparamref name="T"/></returns>
169200
public new static Result Conflict(params string[] errorMessages)
170201
{
171-
return new Result(ResultStatus.Conflict) { Errors = errorMessages };
202+
Result result = new Result(ResultStatus.Conflict);
203+
204+
if (errorMessages != null && errorMessages.Length > 0)
205+
result.Errors = new ObservableCollection<string>(errorMessages);
206+
207+
result.Initialize();
208+
209+
return result;
172210
}
173211

174212
/// <summary>
@@ -180,9 +218,16 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
180218
/// <returns></returns>
181219
public new static Result Unavailable(params string[] errorMessages)
182220
{
183-
return new Result(ResultStatus.Unavailable) { Errors = errorMessages };
221+
Result result = new Result(ResultStatus.Unavailable);
222+
223+
if (errorMessages != null && errorMessages.Length > 0)
224+
result.Errors = new ObservableCollection<string>(errorMessages);
225+
226+
result.Initialize();
227+
228+
return result;
184229
}
185-
230+
186231
/// Represents a critical error that occurred during the execution of the service.
187232
/// Everything provided by the user was valid, but the service was unable to complete due to an exception.
188233
/// See also HTTP 500 Internal Server Error: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors
@@ -191,7 +236,14 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
191236
/// <returns>A Result</returns>
192237
public static Result CriticalError(params string[] errorMessages)
193238
{
194-
return new Result(ResultStatus.CriticalError) { Errors = errorMessages };
239+
Result result = new Result(ResultStatus.CriticalError);
240+
241+
if (errorMessages != null && errorMessages.Length > 0)
242+
result.Errors = new ObservableCollection<string>(errorMessages);
243+
244+
result.Initialize();
245+
246+
return result;
195247
}
196248
}
197249
}

0 commit comments

Comments
 (0)