[CHANGE] Reworking Result monads
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
using DotBased.AspNet.Authority.Models.Validation;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Models;
|
||||
|
||||
public class AuthorityResult<TResultValue> : Result<TResultValue>
|
||||
{
|
||||
public static AuthorityResult<TResultValue> FromResult(Result<TResultValue> result) => new AuthorityResult<TResultValue>(result);
|
||||
|
||||
public AuthorityResult(Result<TResultValue> result) : base(result)
|
||||
{
|
||||
Reason = ResultFailReason.Unknown;
|
||||
}
|
||||
|
||||
public AuthorityResult(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, IReadOnlyList<ValidationError>? errors = null) : base(success, errorMessage, value, null)
|
||||
{
|
||||
Success = success;
|
||||
Message = errorMessage;
|
||||
Value = value;
|
||||
Reason = reason;
|
||||
ValidationErrors = errors;
|
||||
}
|
||||
public ResultFailReason Reason { get; }
|
||||
public IReadOnlyList<ValidationError>? ValidationErrors { get; }
|
||||
|
||||
|
||||
public new static AuthorityResult<TResultValue> Ok(TResultValue? value) => new AuthorityResult<TResultValue>(true, value:value);
|
||||
|
||||
public static AuthorityResult<TResultValue> Error(string errorMessage, ResultFailReason reason = ResultFailReason.Error) =>
|
||||
new AuthorityResult<TResultValue>(false, errorMessage, reason:reason);
|
||||
|
||||
public static AuthorityResult<TResultValue> Failed(IReadOnlyList<ValidationError> errors, ResultFailReason reason = ResultFailReason.None)
|
||||
=> new AuthorityResult<TResultValue>(false, errors:errors, reason:reason);
|
||||
}
|
||||
|
||||
public enum ResultFailReason
|
||||
{
|
||||
None,
|
||||
Unknown,
|
||||
Validation,
|
||||
Error
|
||||
}
|
41
DotBased.AspNet.Authority/Models/AuthorityResultOldOld.cs
Executable file
41
DotBased.AspNet.Authority/Models/AuthorityResultOldOld.cs
Executable file
@@ -0,0 +1,41 @@
|
||||
using DotBased.AspNet.Authority.Models.Validation;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Models;
|
||||
|
||||
public class AuthorityResultOldOld<TResultValue> : ResultOld<TResultValue>
|
||||
{
|
||||
public static AuthorityResultOldOld<TResultValue> FromResult(ResultOld<TResultValue> resultOld) => new AuthorityResultOldOld<TResultValue>(resultOld);
|
||||
|
||||
public AuthorityResultOldOld(ResultOld<TResultValue> resultOld) : base(resultOld)
|
||||
{
|
||||
Reason = ResultFailReason.Unknown;
|
||||
}
|
||||
|
||||
public AuthorityResultOldOld(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, IReadOnlyList<ValidationError>? errors = null) : base(success, errorMessage, value, null)
|
||||
{
|
||||
Success = success;
|
||||
Message = errorMessage;
|
||||
Value = value;
|
||||
Reason = reason;
|
||||
ValidationErrors = errors;
|
||||
}
|
||||
public ResultFailReason Reason { get; }
|
||||
public IReadOnlyList<ValidationError>? ValidationErrors { get; }
|
||||
|
||||
|
||||
public new static AuthorityResultOldOld<TResultValue> Ok(TResultValue? value) => new AuthorityResultOldOld<TResultValue>(true, value:value);
|
||||
|
||||
public static AuthorityResultOldOld<TResultValue> Error(string errorMessage, ResultFailReason reason = ResultFailReason.Error) =>
|
||||
new AuthorityResultOldOld<TResultValue>(false, errorMessage, reason:reason);
|
||||
|
||||
public static AuthorityResultOldOld<TResultValue> Failed(IReadOnlyList<ValidationError> errors, ResultFailReason reason = ResultFailReason.None)
|
||||
=> new AuthorityResultOldOld<TResultValue>(false, errors:errors, reason:reason);
|
||||
}
|
||||
|
||||
public enum ResultFailReason
|
||||
{
|
||||
None,
|
||||
Unknown,
|
||||
Validation,
|
||||
Error
|
||||
}
|
21
DotBased.AspNet.Authority/Models/QueryItems.cs
Normal file
21
DotBased.AspNet.Authority/Models/QueryItems.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace DotBased.AspNet.Authority.Models;
|
||||
|
||||
public class QueryItems<TItem>
|
||||
{
|
||||
private QueryItems(IEnumerable<TItem> items, int totalCount, int limit, int offset)
|
||||
{
|
||||
Items = items.ToList();
|
||||
TotalCount = totalCount;
|
||||
Limit = limit;
|
||||
Offset = offset;
|
||||
}
|
||||
|
||||
public readonly IReadOnlyCollection<TItem> Items;
|
||||
|
||||
public int Count => Items.Count;
|
||||
public int TotalCount { get; }
|
||||
public int Limit { get; }
|
||||
public int Offset { get; }
|
||||
|
||||
public static QueryItems<TItem> Create(IEnumerable<TItem> items, int totalCount, int limit, int offset) => new(items, totalCount, limit, offset);
|
||||
}
|
Reference in New Issue
Block a user