Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
3164d1b2
Commit
3164d1b2
authored
Aug 18, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add result bases
parent
caf7e1cb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
175 additions
and
0 deletions
+175
-0
DomainErrors.cs
Clinics.Backend/Domain/Errors/DomainErrors.cs
+8
-0
Error.cs
Clinics.Backend/Domain/Shared/Error.cs
+65
-0
Result.cs
Clinics.Backend/Domain/Shared/Result.cs
+56
-0
ResultT.cs
Clinics.Backend/Domain/Shared/ResultT.cs
+46
-0
No files found.
Clinics.Backend/Domain/Errors/DomainErrors.cs
0 → 100644
View file @
3164d1b2
using
Domain.Shared
;
namespace
Domain.Errors
;
public
static
class
DomainErrors
{
public
static
Error
InvalidValuesError
=>
new
Error
(
"Domain.InvalidValues"
,
"Given values are invalid!"
);
}
Clinics.Backend/Domain/Shared/Error.cs
0 → 100644
View file @
3164d1b2
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
}
Clinics.Backend/Domain/Shared/Result.cs
0 → 100644
View file @
3164d1b2
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
}
Clinics.Backend/Domain/Shared/ResultT.cs
0 → 100644
View file @
3164d1b2
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
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment