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
61e0e6ec
Commit
61e0e6ec
authored
Aug 16, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add specification pattern
parent
d42c6ffd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
0 deletions
+86
-0
Repositroy.cs
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
+11
-0
Specification.cs
....Backend/Persistence/Specifications/Base/Specification.cs
+36
-0
SpecificationEvaluator.cs
...stence/Specifications/Evaluator/SpecificationEvaluator.cs
+39
-0
No files found.
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
View file @
61e0e6ec
...
...
@@ -19,6 +19,12 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
endregion
#
region
Apply
specification
#
endregion
#
region
CRUD
#
region
Create
operation
public
async
Task
<
TEntity
>
CreateAsync
(
TEntity
entity
)
...
...
@@ -66,6 +72,11 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
endregion
#
endregion
...
...
Clinics.Backend/Persistence/Specifications/Base/Specification.cs
0 → 100644
View file @
61e0e6ec
using
Domain.Primitives
;
using
System.Linq.Expressions
;
namespace
Persistence.Specifications.Base
;
public
abstract
class
Specification
<
TEntity
>
where
TEntity
:
Entity
{
protected
Specification
(
Expression
<
Func
<
TEntity
,
bool
>>?
criteria
)
{
Criteria
=
criteria
;
}
public
Expression
<
Func
<
TEntity
,
bool
>>?
Criteria
{
get
;
}
public
List
<
Expression
<
Func
<
TEntity
,
object
>>>
IncludeExpressions
{
get
;
}
=
new
();
public
Expression
<
Func
<
TEntity
,
object
>>?
OrderByExpression
{
get
;
private
set
;
}
public
Expression
<
Func
<
TEntity
,
object
>>?
OrderByDescendingExpression
{
get
;
private
set
;
}
protected
void
AddInclude
(
Expression
<
Func
<
TEntity
,
object
>>
includeExpression
)
{
IncludeExpressions
.
Add
(
includeExpression
);
}
protected
void
AddOrderBy
(
Expression
<
Func
<
TEntity
,
object
>>
orderByExpression
)
{
OrderByExpression
=
orderByExpression
;
}
protected
void
AddOrderByDescending
(
Expression
<
Func
<
TEntity
,
object
>>
orderByDescendingExpression
)
{
OrderByDescendingExpression
=
orderByDescendingExpression
;
}
}
Clinics.Backend/Persistence/Specifications/Evaluator/SpecificationEvaluator.cs
0 → 100644
View file @
61e0e6ec
using
Domain.Primitives
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Specifications.Base
;
namespace
Persistence.Specifications.Evaluator
;
public
static
class
SpecificationEvaluator
{
public
static
IQueryable
<
TEntity
>
GetQuery
<
TEntity
>(
IQueryable
<
TEntity
>
inputQueryable
,
Specification
<
TEntity
>
specification
)
where
TEntity
:
Entity
{
IQueryable
<
TEntity
>
queryable
=
inputQueryable
;
if
(
specification
.
Criteria
is
not
null
)
{
queryable
=
queryable
.
Where
(
specification
.
Criteria
);
}
specification
.
IncludeExpressions
.
Aggregate
(
queryable
,
(
current
,
includeExpression
)
=>
current
.
Include
(
includeExpression
)
);
if
(
specification
.
OrderByExpression
is
not
null
)
{
queryable
=
queryable
.
OrderBy
(
specification
.
OrderByExpression
);
}
if
(
specification
.
OrderByDescendingExpression
is
not
null
)
{
queryable
=
queryable
.
OrderByDescending
(
specification
.
OrderByDescendingExpression
);
}
return
queryable
;
}
}
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