- Revert ServiceResult to protected set + protected constructors - Remove new() constraint from ValidationBehavior - Use static Failure() factory method via reflection instead
49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
namespace Heimdall.Application.Common;
|
|
|
|
public class ServiceResult
|
|
{
|
|
public bool IsSuccess { get; protected set; }
|
|
public string? Error { get; protected set; }
|
|
public int? StatusCode { get; protected set; }
|
|
|
|
protected ServiceResult() { }
|
|
|
|
public static ServiceResult Success() => new() { IsSuccess = true };
|
|
|
|
public static ServiceResult Failure(string error, int statusCode = 400) =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = statusCode };
|
|
|
|
public static ServiceResult NotFound(string error = "Resource not found") =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 404 };
|
|
|
|
public static ServiceResult Unauthorized(string error = "Unauthorized") =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 401 };
|
|
|
|
public static ServiceResult Conflict(string error) =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 409 };
|
|
}
|
|
|
|
#pragma warning disable CA1000 // Do not declare static members on generic types
|
|
public class ServiceResult<T> : ServiceResult
|
|
{
|
|
public T? Data { get; protected set; }
|
|
|
|
protected ServiceResult() { }
|
|
|
|
public static ServiceResult<T> Success(T data) =>
|
|
new() { IsSuccess = true, Data = data };
|
|
|
|
public new static ServiceResult<T> Failure(string error, int statusCode = 400) =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = statusCode };
|
|
|
|
public new static ServiceResult<T> NotFound(string error = "Resource not found") =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 404 };
|
|
|
|
public new static ServiceResult<T> Unauthorized(string error = "Unauthorized") =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 401 };
|
|
|
|
public new static ServiceResult<T> Conflict(string error) =>
|
|
new() { IsSuccess = false, Error = error, StatusCode = 409 };
|
|
}
|
|
#pragma warning restore CA1000
|