|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | - |
4 | | -namespace Foundatio.Mediator; |
5 | | - |
6 | | -/// <summary> |
7 | | -/// Represents the result of an operation with a return value of type T. |
8 | | -/// </summary> |
9 | | -/// <typeparam name="T">The type of the result value.</typeparam> |
10 | | -public class Result<T> : IResult |
11 | | -{ |
12 | | - /// <summary> |
13 | | - /// Initializes a new instance of the <see cref="Result{T}"/> class. |
14 | | - /// </summary> |
15 | | - protected Result() |
16 | | - { |
17 | | - Value = default!; |
18 | | - } |
19 | | - |
20 | | - /// <summary> |
21 | | - /// Initializes a new instance of the <see cref="Result{T}"/> class with a value. |
22 | | - /// </summary> |
23 | | - /// <param name="value">The result value.</param> |
24 | | - public Result(T value) |
25 | | - { |
26 | | - Value = value; |
27 | | - } |
28 | | - |
29 | | - /// <summary> |
30 | | - /// Initializes a new instance of the <see cref="Result{T}"/> class with a value and success message. |
31 | | - /// </summary> |
32 | | - /// <param name="value">The result value.</param> |
33 | | - /// <param name="successMessage">The success message.</param> |
34 | | - protected internal Result(T value, string successMessage) : this(value) |
35 | | - { |
36 | | - SuccessMessage = successMessage; |
37 | | - } |
38 | | - |
39 | | - /// <summary> |
40 | | - /// Initializes a new instance of the <see cref="Result{T}"/> class with a status. |
41 | | - /// </summary> |
42 | | - /// <param name="status">The result status.</param> |
43 | | - internal Result(ResultStatus status) |
44 | | - { |
45 | | - Status = status; |
46 | | - Value = default!; |
47 | | - } |
48 | | - |
49 | | - /// <summary> |
50 | | - /// Initializes a new instance of the <see cref="Result{T}"/> class with a status and value. |
51 | | - /// </summary> |
52 | | - /// <param name="status">The result status.</param> |
53 | | - /// <param name="value">The result value.</param> |
54 | | - protected Result(ResultStatus status, T value) |
55 | | - { |
56 | | - Status = status; |
57 | | - Value = value; |
58 | | - } |
59 | | - |
60 | | - /// <summary> |
61 | | - /// Implicit conversion from T to Result<T>. |
62 | | - /// </summary> |
63 | | - /// <param name="value">The value to convert.</param> |
64 | | - public static implicit operator Result<T>(T value) => new(value); |
65 | | - |
66 | | - /// <summary> |
67 | | - /// Implicit conversion from Result<T> to T. |
68 | | - /// </summary> |
69 | | - /// <param name="result">The result to convert.</param> |
70 | | - public static implicit operator T(Result<T> result) => result.Value; |
71 | | - |
72 | | - /// <summary> |
73 | | - /// Implicit conversion from Result to Result<T>. |
74 | | - /// </summary> |
75 | | - /// <param name="result">The result to convert.</param> |
76 | | - public static implicit operator Result<T>(Result result) |
77 | | - { |
78 | | - var convertedResult = new Result<T>(result.Status); |
79 | | - convertedResult.Errors = result.Errors; |
80 | | - convertedResult.SuccessMessage = result.SuccessMessage; |
81 | | - convertedResult.CorrelationId = result.CorrelationId; |
82 | | - convertedResult.Location = result.Location; |
83 | | - convertedResult.ValidationErrors = result.ValidationErrors; |
84 | | - return convertedResult; |
85 | | - } |
86 | | - |
87 | | - /// <summary> |
88 | | - /// Gets the result value. |
89 | | - /// </summary> |
90 | | - public T Value { get; private set; } = default!; |
91 | | - |
92 | | - /// <summary> |
93 | | - /// Gets the type of the result value. |
94 | | - /// </summary> |
95 | | - public Type ValueType => typeof(T); |
96 | | - |
97 | | - /// <summary> |
98 | | - /// Gets the status of the result. |
99 | | - /// </summary> |
100 | | - public ResultStatus Status { get; protected set; } = ResultStatus.Ok; |
101 | | - |
102 | | - /// <summary> |
103 | | - /// Gets a value indicating whether the result represents a successful operation. |
104 | | - /// </summary> |
105 | | - public bool IsSuccess => Status == ResultStatus.Ok || Status == ResultStatus.NoContent || Status == ResultStatus.Created; |
106 | | - |
107 | | - /// <summary> |
108 | | - /// Gets the success message, if any. |
109 | | - /// </summary> |
110 | | - public string SuccessMessage { get; internal set; } = string.Empty; |
111 | | - |
112 | | - /// <summary> |
113 | | - /// Gets the correlation ID for tracking purposes. |
114 | | - /// </summary> |
115 | | - public string CorrelationId { get; internal set; } = string.Empty; |
116 | | - |
117 | | - /// <summary> |
118 | | - /// Gets the location of a newly created resource (for Created status). |
119 | | - /// </summary> |
120 | | - public string Location { get; internal set; } = string.Empty; |
121 | | - |
122 | | - /// <summary> |
123 | | - /// Gets the collection of error messages. |
124 | | - /// </summary> |
125 | | - public IEnumerable<string> Errors { get; internal set; } = new List<string>(); |
126 | | - |
127 | | - /// <summary> |
128 | | - /// Gets the collection of validation errors. |
129 | | - /// </summary> |
130 | | - public IEnumerable<ValidationError> ValidationErrors { get; internal set; } = new List<ValidationError>(); |
131 | | - |
132 | | - /// <summary> |
133 | | - /// Gets the result value as an object. |
134 | | - /// </summary> |
135 | | - /// <returns>The result value as an object.</returns> |
136 | | - public object? GetValue() => Value; |
137 | | - |
138 | | - /// <summary> |
139 | | - /// Creates a successful result with a value. |
140 | | - /// </summary> |
141 | | - /// <param name="value">The result value.</param> |
142 | | - /// <returns>A successful result.</returns> |
143 | | - public static Result<T> Success(T value) => new(value); |
144 | | - |
145 | | - /// <summary> |
146 | | - /// Creates a successful result with a value and success message. |
147 | | - /// </summary> |
148 | | - /// <param name="value">The result value.</param> |
149 | | - /// <param name="successMessage">The success message.</param> |
150 | | - /// <returns>A successful result.</returns> |
151 | | - public static Result<T> Success(T value, string successMessage) => new(value, successMessage); |
152 | | - |
153 | | - /// <summary> |
154 | | - /// Creates a result indicating successful creation of a resource. |
155 | | - /// </summary> |
156 | | - /// <param name="value">The created resource.</param> |
157 | | - /// <returns>A result with Created status.</returns> |
158 | | - public static Result<T> Created(T value) => new(ResultStatus.Created, value); |
159 | | - |
160 | | - /// <summary> |
161 | | - /// Creates a result indicating successful creation of a resource with a location. |
162 | | - /// </summary> |
163 | | - /// <param name="value">The created resource.</param> |
164 | | - /// <param name="location">The location of the created resource.</param> |
165 | | - /// <returns>A result with Created status and location.</returns> |
166 | | - public static Result<T> Created(T value, string location) |
167 | | - { |
168 | | - var result = new Result<T>(ResultStatus.Created, value); |
169 | | - result.Location = location; |
170 | | - return result; |
171 | | - } |
172 | | - |
173 | | - /// <summary> |
174 | | - /// Creates a result indicating no content. |
175 | | - /// </summary> |
176 | | - /// <returns>A result with NoContent status.</returns> |
177 | | - public static Result<T> NoContent() => new(ResultStatus.NoContent); |
178 | | - |
179 | | - /// <summary> |
180 | | - /// Creates an error result with a single error message. |
181 | | - /// </summary> |
182 | | - /// <param name="errorMessage">The error message.</param> |
183 | | - /// <returns>An error result.</returns> |
184 | | - public static Result<T> Error(string errorMessage) |
185 | | - { |
186 | | - var result = new Result<T>(ResultStatus.Error); |
187 | | - result.Errors = new List<string> { errorMessage }; |
188 | | - return result; |
189 | | - } |
190 | | - |
191 | | - /// <summary> |
192 | | - /// Creates an error result with multiple error messages. |
193 | | - /// </summary> |
194 | | - /// <param name="errorMessages">The error messages.</param> |
195 | | - /// <returns>An error result.</returns> |
196 | | - public static Result<T> Error(params string[] errorMessages) |
197 | | - { |
198 | | - var result = new Result<T>(ResultStatus.Error); |
199 | | - result.Errors = errorMessages; |
200 | | - return result; |
201 | | - } |
202 | | - |
203 | | - /// <summary> |
204 | | - /// Creates an error result with error messages. |
205 | | - /// </summary> |
206 | | - /// <param name="errorMessages">The error messages.</param> |
207 | | - /// <returns>An error result.</returns> |
208 | | - public static Result<T> Error(IEnumerable<string> errorMessages) |
209 | | - { |
210 | | - var result = new Result<T>(ResultStatus.Error); |
211 | | - result.Errors = errorMessages; |
212 | | - return result; |
213 | | - } |
214 | | - |
215 | | - /// <summary> |
216 | | - /// Creates an invalid result with a single validation error. |
217 | | - /// </summary> |
218 | | - /// <param name="validationError">The validation error.</param> |
219 | | - /// <returns>An invalid result.</returns> |
220 | | - public static Result<T> Invalid(ValidationError validationError) |
221 | | - { |
222 | | - var result = new Result<T>(ResultStatus.Invalid); |
223 | | - result.ValidationErrors = new List<ValidationError> { validationError }; |
224 | | - return result; |
225 | | - } |
226 | | - |
227 | | - /// <summary> |
228 | | - /// Creates an invalid result with multiple validation errors. |
229 | | - /// </summary> |
230 | | - /// <param name="validationErrors">The validation errors.</param> |
231 | | - /// <returns>An invalid result.</returns> |
232 | | - public static Result<T> Invalid(params ValidationError[] validationErrors) |
233 | | - { |
234 | | - var result = new Result<T>(ResultStatus.Invalid); |
235 | | - result.ValidationErrors = validationErrors; |
236 | | - return result; |
237 | | - } |
238 | | - |
239 | | - /// <summary> |
240 | | - /// Creates an invalid result with validation errors. |
241 | | - /// </summary> |
242 | | - /// <param name="validationErrors">The validation errors.</param> |
243 | | - /// <returns>An invalid result.</returns> |
244 | | - public static Result<T> Invalid(IEnumerable<ValidationError> validationErrors) |
245 | | - { |
246 | | - var result = new Result<T>(ResultStatus.Invalid); |
247 | | - result.ValidationErrors = validationErrors; |
248 | | - return result; |
249 | | - } |
250 | | - |
251 | | - /// <summary> |
252 | | - /// Creates a not found result. |
253 | | - /// </summary> |
254 | | - /// <returns>A not found result.</returns> |
255 | | - public static Result<T> NotFound() => new(ResultStatus.NotFound); |
256 | | - |
257 | | - /// <summary> |
258 | | - /// Creates a not found result with error messages. |
259 | | - /// </summary> |
260 | | - /// <param name="errorMessages">The error messages.</param> |
261 | | - /// <returns>A not found result.</returns> |
262 | | - public static Result<T> NotFound(params string[] errorMessages) |
263 | | - { |
264 | | - var result = new Result<T>(ResultStatus.NotFound); |
265 | | - result.Errors = errorMessages; |
266 | | - return result; |
267 | | - } |
268 | | - |
269 | | - /// <summary> |
270 | | - /// Creates an unauthorized result. |
271 | | - /// </summary> |
272 | | - /// <returns>An unauthorized result.</returns> |
273 | | - public static Result<T> Unauthorized() => new(ResultStatus.Unauthorized); |
274 | | - |
275 | | - /// <summary> |
276 | | - /// Creates an unauthorized result with error messages. |
277 | | - /// </summary> |
278 | | - /// <param name="errorMessages">The error messages.</param> |
279 | | - /// <returns>An unauthorized result.</returns> |
280 | | - public static Result<T> Unauthorized(params string[] errorMessages) |
281 | | - { |
282 | | - var result = new Result<T>(ResultStatus.Unauthorized); |
283 | | - result.Errors = errorMessages; |
284 | | - return result; |
285 | | - } |
286 | | - |
287 | | - /// <summary> |
288 | | - /// Creates a forbidden result. |
289 | | - /// </summary> |
290 | | - /// <returns>A forbidden result.</returns> |
291 | | - public static Result<T> Forbidden() => new(ResultStatus.Forbidden); |
292 | | - |
293 | | - /// <summary> |
294 | | - /// Creates a forbidden result with error messages. |
295 | | - /// </summary> |
296 | | - /// <param name="errorMessages">The error messages.</param> |
297 | | - /// <returns>A forbidden result.</returns> |
298 | | - public static Result<T> Forbidden(params string[] errorMessages) |
299 | | - { |
300 | | - var result = new Result<T>(ResultStatus.Forbidden); |
301 | | - result.Errors = errorMessages; |
302 | | - return result; |
303 | | - } |
304 | | - |
305 | | - /// <summary> |
306 | | - /// Creates a conflict result. |
307 | | - /// </summary> |
308 | | - /// <returns>A conflict result.</returns> |
309 | | - public static Result<T> Conflict() => new(ResultStatus.Conflict); |
310 | | - |
311 | | - /// <summary> |
312 | | - /// Creates a conflict result with error messages. |
313 | | - /// </summary> |
314 | | - /// <param name="errorMessages">The error messages.</param> |
315 | | - /// <returns>A conflict result.</returns> |
316 | | - public static Result<T> Conflict(params string[] errorMessages) |
317 | | - { |
318 | | - var result = new Result<T>(ResultStatus.Conflict); |
319 | | - result.Errors = errorMessages; |
320 | | - return result; |
321 | | - } |
322 | | - |
323 | | - /// <summary> |
324 | | - /// Creates a critical error result. |
325 | | - /// </summary> |
326 | | - /// <param name="errorMessages">The error messages.</param> |
327 | | - /// <returns>A critical error result.</returns> |
328 | | - public static Result<T> CriticalError(params string[] errorMessages) |
329 | | - { |
330 | | - var result = new Result<T>(ResultStatus.CriticalError); |
331 | | - result.Errors = errorMessages; |
332 | | - return result; |
333 | | - } |
334 | | - |
335 | | - /// <summary> |
336 | | - /// Creates an unavailable result. |
337 | | - /// </summary> |
338 | | - /// <param name="errorMessages">The error messages.</param> |
339 | | - /// <returns>An unavailable result.</returns> |
340 | | - public static Result<T> Unavailable(params string[] errorMessages) |
341 | | - { |
342 | | - var result = new Result<T>(ResultStatus.Unavailable); |
343 | | - result.Errors = errorMessages; |
344 | | - return result; |
345 | | - } |
346 | | -} |
0 commit comments