Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
ProjectsStatusManagement
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
hasan.bahjat
ProjectsStatusManagement
Commits
5fd9fdbd
Commit
5fd9fdbd
authored
Jul 16, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add REsult Errorr For REsult Patern.
parent
c88a286c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
0 deletions
+78
-0
Error.cs
PSManagement.SharedKernel/Utilities/Error.cs
+15
-0
Result.cs
PSManagement.SharedKernel/Utilities/Result.cs
+63
-0
No files found.
PSManagement.SharedKernel/Utilities/Error.cs
0 → 100644
View file @
5fd9fdbd
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
PSManagement.SharedKernel.Utilities
{
public
record
Error
(
string
Code
,
string
Name
)
{
public
static
Error
None
=
new
(
string
.
Empty
,
string
.
Empty
);
public
static
Error
NullValue
=
new
(
"Error.NullValue"
,
"Null value was provided"
);
}
}
PSManagement.SharedKernel/Utilities/Result.cs
0 → 100644
View file @
5fd9fdbd
using
System
;
using
System.Collections.Generic
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
PSManagement.SharedKernel.Utilities
{
public
class
Result
{
protected
internal
Result
(
bool
isSuccess
,
Error
error
)
{
if
(
isSuccess
&&
error
!=
Error
.
None
)
{
throw
new
InvalidOperationException
();
}
if
(!
isSuccess
&&
error
==
Error
.
None
)
{
throw
new
InvalidOperationException
();
}
IsSuccess
=
isSuccess
;
Error
=
error
;
}
public
bool
IsSuccess
{
get
;
}
public
bool
IsFailure
=>
!
IsSuccess
;
public
Error
Error
{
get
;
}
public
static
Result
Success
()
=>
new
(
true
,
Error
.
None
);
public
static
Result
Failure
(
Error
error
)
=>
new
(
false
,
error
);
public
static
Result
<
TValue
>
Success
<
TValue
>(
TValue
value
)
=>
new
(
value
,
true
,
Error
.
None
);
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
);
}
public
class
Result
<
TValue
>
:
Result
{
private
readonly
TValue
?
_value
;
protected
internal
Result
(
TValue
?
value
,
bool
isSuccess
,
Error
error
)
:
base
(
isSuccess
,
error
)
{
_value
=
value
;
}
[
NotNull
]
public
TValue
Value
=>
IsSuccess
?
_value
!
:
throw
new
InvalidOperationException
(
"The value of a failure result can not be accessed."
);
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