1- using System . Diagnostics . CodeAnalysis ;
1+ using System . Text . Json . Serialization ;
22using Riber . Domain . Abstractions ;
33
44namespace Riber . Application . Common ;
55
66public class Result
77{
8+ #region Properties
9+
10+ [ JsonInclude ] public bool IsSuccess { get ; init ; }
11+ [ JsonInclude ] public Error Error { get ; init ; } = new ( ) ;
12+
13+ #endregion
14+
15+ #region Constructors
16+
17+ [ JsonConstructor ]
18+ protected Result ( ) { }
19+
820 protected Result ( bool isSuccess , Error error )
921 {
10- switch ( isSuccess )
11- {
12- case true when error != Error . None :
13- case false when error == Error . None :
14- throw new InvalidOperationException ( ) ;
15-
16- default :
17- IsSuccess = isSuccess ;
18- Error = error ;
19- break ;
20- }
22+ IsSuccess = isSuccess ;
23+ Error = error ;
2124 }
2225
23- public bool IsSuccess { get ; }
24- public Error Error { get ; }
26+ #endregion
2527
26- public static Result Success ( ) => new ( true , Error . None ) ;
27- public static Result < T > Success < T > ( T value ) => new ( value , true , Error . None ) ;
28+ #region Static Methods
2829
29- public static Result Failure ( Error error ) => new ( false , error ) ;
30+ public static Result < object > Success ( ) => new ( null , true , new Error ( ) ) ;
31+ public static Result < T > Success < T > ( T value ) => new ( value , true , new Error ( ) ) ;
3032 public static Result < T > Failure < T > ( Error error ) => new ( default , false , error ) ;
3133
32- public static Result < T > Create < T > ( T ? value ) =>
33- value is not null ? Success ( value ) : Failure < T > ( Error . NullValue ) ;
34+ protected static Result < T > Create < T > ( T ? value ) =>
35+ value is not null ? Success ( value ) : Failure < T > ( new Error ( ) ) ;
36+
37+ #endregion
3438}
3539
3640public class Result < T > : Result
3741{
38- private readonly T ? _value ;
42+ #region Properties
43+
44+ [ JsonInclude ]
45+ [ JsonIgnore ( Condition = JsonIgnoreCondition . Never ) ]
46+ public T ? Value { get ; init ; }
47+
48+ #endregion
49+
50+ #region Constructors
51+
52+ [ JsonConstructor ]
53+ protected Result ( ) { }
3954
4055 protected internal Result ( T ? value , bool isSuccess , Error error ) : base ( isSuccess , error )
41- => _value = value ;
56+ => Value = value ;
57+
58+ #endregion
59+
60+ #region Overrides
4261
43- [ NotNull ]
44- public T Value => _value ! ?? throw new InvalidOperationException ( "Result has no value" ) ;
62+ public static implicit operator Result < T > ( T ? value )
63+ => Create ( value ) ;
4564
46- public static implicit operator Result < T > ( T ? value ) => Create ( value ) ;
65+ #endregion
4766}
0 commit comments