-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resultpattern): add result pattern
- Loading branch information
Showing
10 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/DistributionCenter.Commons/DistributionCenter.Commons.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace DistributionCenter.Commons.Enums; | ||
|
||
public enum ErrorType | ||
{ | ||
Conflict, | ||
Validation, | ||
NotFound, | ||
Unauthorized, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace DistributionCenter.Commons.Errors; | ||
|
||
using DistributionCenter.Commons.Enums; | ||
using DistributionCenter.Commons.Errors.Interfaces; | ||
|
||
#pragma warning disable CA1716 | ||
public readonly record struct Error : IError | ||
{ | ||
public string Code { get; } | ||
|
||
public string Description { get; } | ||
|
||
public ErrorType Type { get; } | ||
|
||
private Error(string code, string description, ErrorType type) | ||
{ | ||
Code = code; | ||
Description = description; | ||
Type = type; | ||
} | ||
|
||
public static IError Conflict( | ||
string code = "General.Conflict", | ||
string description = "A 'Conflict' error has occurred" | ||
) | ||
{ | ||
return new Error(code, description, ErrorType.Conflict); | ||
} | ||
|
||
public static IError Validation( | ||
string code = "General.Validation", | ||
string description = "A 'Validation' error has occurred" | ||
) | ||
{ | ||
return new Error(code, description, ErrorType.Validation); | ||
} | ||
|
||
public static IError NotFound( | ||
string code = "General.NotFound", | ||
string description = "A 'Not Found' error has occurred" | ||
) | ||
{ | ||
return new Error(code, description, ErrorType.NotFound); | ||
} | ||
|
||
public static IError Unauthorized( | ||
string code = "General.Unauthorized", | ||
string description = "A 'Unauthorized' error has occurred" | ||
) | ||
{ | ||
return new Error(code, description, ErrorType.Unauthorized); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/DistributionCenter.Commons/Errors/Interfaces/IError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace DistributionCenter.Commons.Errors.Interfaces; | ||
|
||
using DistributionCenter.Commons.Enums; | ||
|
||
public interface IError | ||
{ | ||
string Code { get; } | ||
|
||
string Description { get; } | ||
|
||
ErrorType Type { get; } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/DistributionCenter.Commons/Results/Interfaces/IResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace DistributionCenter.Commons.Results.Interfaces; | ||
|
||
using DistributionCenter.Commons.Errors.Interfaces; | ||
|
||
public interface IResult | ||
{ | ||
bool IsSuccess { get; } | ||
|
||
ICollection<IError> Errors { get; } | ||
} | ||
|
||
public interface IResult<T> : IResult | ||
{ | ||
T Value { get; } | ||
|
||
TNext Match<TNext>(Func<T, TNext> success, Func<ICollection<IError>, TNext> failure); | ||
|
||
Task<TNext> MatchAsync<TNext>(Func<T, Task<TNext>> success, Func<ICollection<IError>, Task<TNext>> failure); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace DistributionCenter.Commons.Results; | ||
|
||
using System.Collections.ObjectModel; | ||
using DistributionCenter.Commons.Errors; | ||
using DistributionCenter.Commons.Errors.Interfaces; | ||
using DistributionCenter.Commons.Results.Interfaces; | ||
|
||
public partial class Result : IResult | ||
{ | ||
public static Result Ok() | ||
{ | ||
return new Result(); | ||
} | ||
|
||
public static implicit operator Result(Error error) | ||
{ | ||
return new Result(error); | ||
} | ||
|
||
public static implicit operator Result(Collection<IError> errors) | ||
{ | ||
return new Result(errors); | ||
} | ||
} | ||
|
||
public partial class Result<T> : Result, IResult<T> | ||
{ | ||
public static implicit operator Result<T>(T value) | ||
{ | ||
return new Result<T>(value); | ||
} | ||
|
||
public static implicit operator Result<T>(Error error) | ||
{ | ||
return new Result<T>(error); | ||
} | ||
|
||
public static implicit operator Result<T>(Collection<IError> errors) | ||
{ | ||
return new Result<T>(errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace DistributionCenter.Commons.Results; | ||
|
||
using DistributionCenter.Commons.Errors.Interfaces; | ||
using DistributionCenter.Commons.Results.Interfaces; | ||
|
||
public partial class Result<T> : Result, IResult<T> | ||
{ | ||
public TNext Match<TNext>(Func<T, TNext> success, Func<ICollection<IError>, TNext> failure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(success, nameof(success)); | ||
ArgumentNullException.ThrowIfNull(failure, nameof(failure)); | ||
|
||
return IsSuccess ? success(Value) : failure(Errors); | ||
} | ||
|
||
public Task<TNext> MatchAsync<TNext>(Func<T, Task<TNext>> success, Func<ICollection<IError>, Task<TNext>> failure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(success, nameof(success)); | ||
ArgumentNullException.ThrowIfNull(failure, nameof(failure)); | ||
|
||
return IsSuccess ? success(Value) : failure(Errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace DistributionCenter.Commons.Results; | ||
|
||
using DistributionCenter.Commons.Errors.Interfaces; | ||
using DistributionCenter.Commons.Results.Interfaces; | ||
|
||
public partial class Result : IResult | ||
{ | ||
private readonly ICollection<IError>? _errors; | ||
|
||
protected Result() { } | ||
|
||
protected Result(IError error) | ||
{ | ||
_errors = [error]; | ||
} | ||
|
||
protected Result(ICollection<IError> errors) | ||
{ | ||
_errors = errors; | ||
} | ||
|
||
public bool IsSuccess => _errors is null || _errors.Count == 0; | ||
|
||
public ICollection<IError> Errors | ||
{ | ||
get | ||
{ | ||
if (IsSuccess) | ||
{ | ||
throw new InvalidOperationException("Cannot access errors when there are no errors."); | ||
} | ||
|
||
return _errors!; | ||
} | ||
} | ||
} | ||
|
||
public partial class Result<T> : Result, IResult<T> | ||
{ | ||
private readonly T? _value; | ||
|
||
private Result(T value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
private Result(IError error) | ||
: base(error) { } | ||
|
||
private Result(ICollection<IError> errors) | ||
: base(errors) { } | ||
|
||
public T Value | ||
{ | ||
get | ||
{ | ||
if (!IsSuccess || _value is null) | ||
{ | ||
throw new InvalidOperationException("Cannot access value when there are errors."); | ||
} | ||
|
||
return _value; | ||
} | ||
} | ||
} |