Commit 3164d1b2 authored by Almouhannad's avatar Almouhannad

(B) Add result bases

parent caf7e1cb
using Domain.Shared;
namespace Domain.Errors;
public static class DomainErrors
{
public static Error InvalidValuesError => new Error("Domain.InvalidValues", "Given values are invalid!");
}
namespace Domain.Shared;
public class Error : IEquatable<Error>
{
#region Ctor
public Error(string code, string message)
{
Code = code;
Message = message;
}
#endregion
#region Properties
public string Code { get; }
public string Message { get; }
#endregion
#region Methods
#region Equality by value
public static bool operator ==(Error? a, Error? b)
{
if (a is null && b is null)
{
return true;
}
if (a is null || b is null)
{
return false;
}
return a.Equals(b);
}
public static bool operator !=(Error? a, Error? b) => !(a == b);
public virtual bool Equals(Error? other)
{
if (other is null)
{
return false;
}
return Code == other.Code && Message == other.Message;
}
public override bool Equals(object? obj) => obj is Error error && Equals(error);
#endregion
#region Hash code
public override int GetHashCode() => HashCode.Combine(Code, Message);
#endregion
public static implicit operator string(Error error) => error.Code;
public override string ToString() => Code;
#endregion
#region Statics
public static readonly Error None = new(string.Empty, string.Empty);
public static readonly Error NullValue = new("Error.NullValue", "The specified result value is null.");
#endregion
}
namespace Domain.Shared;
public class Result
{
#region Ctor
public Result(bool isSuccess, Error error)
{
IsSuccess = isSuccess;
Error = error;
}
#endregion
#region Properties
public bool IsSuccess { get; }
public bool IsFailure => !IsSuccess;
public Error Error { get; }
#endregion
#region Methods
#region Static factory
public static Result Create(bool isSuccess, Error error)
{
if (isSuccess && error != Error.None)
{
throw new InvalidOperationException();
}
if (!isSuccess && error == Error.None)
{
throw new InvalidOperationException();
}
return new Result(isSuccess, error);
}
#endregion
#endregion
#region Statics
public static Result Success() => new(true, Error.None);
public static Result<TValue> Success<TValue>(TValue value) => new(value, true, Error.None);
public static Result Failure(Error error) => new(false, error);
public static Result<TValue> Failure<TValue>(Error error) => new(default, false, error);
public static Result<TValue> Create<TValue>(TValue? value) => value is not null ? Success(value) : Failure<TValue>(Error.NullValue);
#endregion
}
namespace Domain.Shared;
public class Result<TValue> : Result
{
#region Ctor
public Result(TValue? value, bool isSuccess, Error error) : base(isSuccess, error)
{
_value = value;
}
#endregion
#region Properties
private readonly TValue? _value;
public TValue Value => IsSuccess
? _value!
: throw new InvalidOperationException("The value of a failure result can not be accessed.");
#endregion
#region Methods
#region Static factory
public static Result<TValue> Create(TValue? value, bool isSuccess, Error error)
{
try
{
Result.Create(isSuccess, error);
}
catch (Exception)
{
throw;
}
return new Result<TValue>(value, isSuccess, error);
}
#endregion
#endregion
// To allow returning a result without required castring
public static implicit operator Result<TValue>(TValue? value) => Create(value);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment