wip
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Error(string Code, string Message)
|
||||
public record Reason(string Code, string Message)
|
||||
{
|
||||
public static readonly Error None = new(string.Empty, string.Empty);
|
||||
public static readonly Reason None = new(string.Empty, string.Empty);
|
||||
|
||||
public static readonly Error Null = new("Error.NullValue", "The specified result value is null.");
|
||||
public static readonly Reason Null = new("Error.NullValue", "The specified result value is null.");
|
||||
|
||||
public static readonly Error ConditionNotMet = new("Error.ConditionNotMet", "The specified condition was not met.");
|
||||
public static readonly Reason ConditionNotMet = new("Error.ConditionNotMet", "The specified condition was not met.");
|
||||
|
||||
public static readonly Error Duplicated = new("Error.Duplicated", "The specified item already exists.");
|
||||
|
||||
public static readonly Error Failure = new("Error.Failure", "The operation has failed.");
|
||||
public static readonly Reason Failure = new("Error.Failure", "The operation has failed.");
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record RequestEventArgs<TSender>
|
||||
public record RequestEventArgs
|
||||
{
|
||||
public TSender? Sender { get; }
|
||||
public object? Sender { get; init; }
|
||||
|
||||
public RequestEventArgs(TSender sender)
|
||||
public RequestEventArgs(object? sender = null)
|
||||
{
|
||||
Sender = sender;
|
||||
}
|
||||
}
|
||||
|
||||
public RequestEventArgs()
|
||||
public record RequestEventArgs<TSender> : RequestEventArgs
|
||||
{
|
||||
public RequestEventArgs(TSender sender) : base(sender)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,53 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Result<TValue> :
|
||||
Result
|
||||
public record Result<TValue> : Result
|
||||
{
|
||||
private readonly TValue? value;
|
||||
public TValue? Value { get; }
|
||||
|
||||
protected internal Result(TValue? value, bool isSuccess, Error error)
|
||||
: base(isSuccess, error) => this.value = value;
|
||||
public Result(TValue? value, bool isSuccess, Reason reason)
|
||||
: base(isSuccess, reason)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public TValue? Value =>
|
||||
IsSuccess ? value! : default;
|
||||
|
||||
public static implicit operator Result<TValue>(TValue? value) =>
|
||||
public static implicit operator Result<TValue>(TValue? value) =>
|
||||
Create(value);
|
||||
|
||||
public static implicit operator TValue?(Result<TValue> result) =>
|
||||
result.IsSuccess ? result.Value : default;
|
||||
}
|
||||
|
||||
public record Result(bool IsSuccess,
|
||||
Error Error)
|
||||
public record Result
|
||||
{
|
||||
public bool IsSuccess { get; init; }
|
||||
|
||||
public bool IsFailure => !IsSuccess;
|
||||
|
||||
public static Result Success() => new(true, Error.None);
|
||||
public Reason Reason { get; init; } = Reason.None;
|
||||
|
||||
public static Result<TValue> Success<TValue>(TValue value) =>
|
||||
new(value, true, Error.None);
|
||||
protected Result(bool isSuccess, Reason reason)
|
||||
{
|
||||
IsSuccess = isSuccess;
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
public static Result Failure(Error error) =>
|
||||
new(false, error);
|
||||
public static Result Success() => new(true, Reason.None);
|
||||
|
||||
public static Result<TValue> Failure<TValue>(Error error) =>
|
||||
new(default, false, error);
|
||||
public static Result Failure(Reason reason) => new(false, reason);
|
||||
|
||||
public static Result Create(bool condition) =>
|
||||
condition ? Success() : Failure(Error.ConditionNotMet);
|
||||
public static Result Create(bool condition) =>
|
||||
condition ? Success() : Failure(Reason.ConditionNotMet);
|
||||
|
||||
public static Result<TValue> Create<TValue>(TValue? value) =>
|
||||
value is not null ? Success(value) : Failure<TValue>(Error.Null);
|
||||
}
|
||||
public static Result<TValue> Success<TValue>(TValue value) =>
|
||||
new(value, true, Reason.None);
|
||||
|
||||
public static Result<TValue> Failure<TValue>(Reason reason, TValue? value = default) =>
|
||||
new(value, false, reason);
|
||||
|
||||
public static Result<TValue> Create<TValue>(TValue? value) =>
|
||||
value is not null ? Success(value) : Failure<TValue>(Reason.Null);
|
||||
|
||||
public static Result Empty() => Failure(Reason.None);
|
||||
|
||||
public static Result<TValue> Empty<TValue>() => Failure<TValue>(Reason.None);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public static class ResultExtensions
|
||||
{
|
||||
public static TResult Failure<TResult>(this object? sender,
|
||||
Reason reason)
|
||||
where TResult : Result
|
||||
{
|
||||
if (typeof(TResult).IsGenericType && typeof(TResult).GetGenericTypeDefinition() == typeof(Result<>))
|
||||
{
|
||||
object? value = sender is not null && typeof(TResult).GetGenericArguments()[0].IsAssignableFrom(sender.GetType())
|
||||
? sender : Activator.CreateInstance(typeof(TResult).GetGenericArguments()[0].MakeNullable());
|
||||
|
||||
return (TResult)Activator.CreateInstance(typeof(TResult), value, false, reason)!;
|
||||
}
|
||||
|
||||
return (TResult)Activator.CreateInstance(typeof(TResult), false, reason)!;
|
||||
}
|
||||
|
||||
public static TResult Success<TResult>(this object? sender)
|
||||
where TResult : Result
|
||||
{
|
||||
if (typeof(TResult).IsGenericType && typeof(TResult).GetGenericTypeDefinition() == typeof(Result<>))
|
||||
{
|
||||
object? value = sender is not null && typeof(TResult).GetGenericArguments()[0].IsAssignableFrom(sender.GetType())
|
||||
? sender : Activator.CreateInstance(typeof(TResult).GetGenericArguments()[0].MakeNullable());
|
||||
|
||||
return (TResult)Activator.CreateInstance(typeof(TResult), value, true, Reason.None)!;
|
||||
}
|
||||
|
||||
return (TResult)Activator.CreateInstance(typeof(TResult), true, Reason.None)!;
|
||||
}
|
||||
|
||||
public static TResult Create<TResult>(this object? sender, bool condition)
|
||||
where TResult : Result => condition ? sender.Success<TResult>() : sender.Failure<TResult>(Reason.ConditionNotMet);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public static class TypeExtensions
|
||||
{
|
||||
public static Type MakeNullable(this Type type) =>
|
||||
type.IsValueType && Nullable.GetUnderlyingType(type) == null
|
||||
? typeof(Nullable<>).MakeGenericType(type)
|
||||
: type;
|
||||
}
|
||||
Reference in New Issue
Block a user