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
a23dc773
Commit
a23dc773
authored
Aug 17, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add unit of work
parent
6529f82d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
60 additions
and
29 deletions
+60
-29
IRepository.cs
Clinics.Backend/Domain/Repositories/Base/IRepository.cs
+6
-4
IUnitOfWork.cs
Clinics.Backend/Domain/UnitOfWork/IUnitOfWork.cs
+6
-0
Repositroy.cs
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
+18
-22
Specification.cs
...istence/Repositories/Specifications/Base/Specification.cs
+1
-1
SpecificationEvaluator.cs
...tories/Specifications/Evaluator/SpecificationEvaluator.cs
+2
-2
UnitOfWork.cs
Clinics.Backend/Persistence/UnitOfWork/UnitOfWork.cs
+27
-0
No files found.
Clinics.Backend/Domain/Repositories/Base/IRepository.cs
View file @
a23dc773
...
@@ -2,14 +2,16 @@
...
@@ -2,14 +2,16 @@
namespace
Domain.Repositories.Base
namespace
Domain.Repositories.Base
{
{
// CRUD operations
// Note that queries are async, but commands are NOT
// Since the persist operation is done by UnitOfWork
public
interface
IRepository
<
TEntity
>
public
interface
IRepository
<
TEntity
>
where
TEntity
:
Entity
where
TEntity
:
Entity
{
{
// CRUD operations
#
region
Create
operation
#
region
Create
operation
public
Task
<
TEntity
>
CreateAsync
(
TEntity
entity
);
public
void
Create
(
TEntity
entity
);
#
endregion
#
endregion
...
@@ -23,13 +25,13 @@ namespace Domain.Repositories.Base
...
@@ -23,13 +25,13 @@ namespace Domain.Repositories.Base
#
region
Update
oprtation
#
region
Update
oprtation
public
Task
<
TEntity
>
UpdateAsync
(
TEntity
entity
);
public
void
Update
(
TEntity
entity
);
#
endregion
#
endregion
#
region
Delete
operation
#
region
Delete
operation
public
Task
DeleteAsync
(
int
id
);
public
void
Delete
(
TEntity
entity
);
#
endregion
#
endregion
}
}
...
...
Clinics.Backend/Domain/UnitOfWork/IUnitOfWork.cs
0 → 100644
View file @
a23dc773
namespace
Domain.UnitOfWork
;
public
interface
IUnitOfWork
{
public
Task
SaveChangesAsync
();
}
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
View file @
a23dc773
using
Domain.Primitives
;
using
Domain.Primitives
;
using
Domain.Repositories.Base
;
using
Domain.Repositories.Base
;
using
Domain.UnitOfWork
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Context
;
using
Persistence.Context
;
using
Persistence.Repositories.Specifications.Base
;
using
Persistence.Repositories.Specifications.Evaluator
;
namespace
Persistence.Repositories.Base
;
namespace
Persistence.Repositories.Base
;
...
@@ -10,9 +13,9 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
...
@@ -10,9 +13,9 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
region
Ctor
DI
for
context
#
region
Ctor
DI
for
context
pr
ivate
readonly
ClinicsDbContext
_context
;
pr
otected
readonly
ClinicsDbContext
_context
;
public
Repositroy
(
ClinicsDbContext
context
)
public
Repositroy
(
ClinicsDbContext
context
,
IUnitOfWork
unitOfWork
)
{
{
_context
=
context
;
_context
=
context
;
}
}
...
@@ -20,30 +23,31 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
...
@@ -20,30 +23,31 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
endregion
#
endregion
#
region
Apply
specification
#
region
Apply
specification
protected
IQueryable
<
TEntity
>
ApplySpecification
(
Specification
<
TEntity
>
specification
)
{
return
SpecificationEvaluator
.
GetQuery
(
_context
.
Set
<
TEntity
>(),
specification
);
}
#
endregion
#
endregion
#
region
CRUD
// CRUD operations
#
region
Create
operation
#
region
Create
operation
public
async
Task
<
TEntity
>
CreateAsync
(
TEntity
entity
)
public
virtual
void
Create
(
TEntity
entity
)
{
{
var
query
=
await
_context
.
Set
<
TEntity
>().
AddAsync
(
entity
);
_context
.
Set
<
TEntity
>().
Add
(
entity
);
await
_context
.
SaveChangesAsync
();
return
query
.
Entity
;
}
}
#
endregion
#
endregion
#
region
Read
operations
#
region
Read
operations
public
async
Task
<
TEntity
?>
GetByIdAsync
(
int
id
)
public
virtual
async
Task
<
TEntity
?>
GetByIdAsync
(
int
id
)
{
{
return
await
_context
.
Set
<
TEntity
>().
FindAsync
(
id
);
return
await
_context
.
Set
<
TEntity
>().
FindAsync
(
id
);
}
}
public
async
Task
<
ICollection
<
TEntity
>>
GetAllAsync
()
public
virtual
async
Task
<
ICollection
<
TEntity
>>
GetAllAsync
()
{
{
return
await
_context
.
Set
<
TEntity
>().
ToListAsync
();
return
await
_context
.
Set
<
TEntity
>().
ToListAsync
();
}
}
...
@@ -52,28 +56,20 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
...
@@ -52,28 +56,20 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
region
Update
operation
#
region
Update
operation
public
async
Task
<
TEntity
>
UpdateAsync
(
TEntity
entity
)
public
virtual
void
Update
(
TEntity
entity
)
{
{
var
query
=
_context
.
Set
<
TEntity
>().
Update
(
entity
);
_context
.
Set
<
TEntity
>().
Update
(
entity
);
await
_context
.
SaveChangesAsync
();
return
query
.
Entity
;
}
}
#
endregion
#
endregion
#
region
Delete
operation
#
region
Delete
operation
public
async
Task
DeleteAsync
(
int
id
)
public
virtual
void
Delete
(
TEntity
entity
)
{
{
var
entity
=
await
GetByIdAsync
(
id
);
if
(
entity
is
not
null
)
_context
.
Set
<
TEntity
>().
Remove
(
entity
);
_context
.
Set
<
TEntity
>().
Remove
(
entity
);
await
_context
.
SaveChangesAsync
();
}
}
#
endregion
#
endregion
#
endregion
}
}
Clinics.Backend/Persistence/Specifications/Base/Specification.cs
→
Clinics.Backend/Persistence/
Repositories/
Specifications/Base/Specification.cs
View file @
a23dc773
using
Domain.Primitives
;
using
Domain.Primitives
;
using
System.Linq.Expressions
;
using
System.Linq.Expressions
;
namespace
Persistence.Specifications.Base
;
namespace
Persistence.
Repositories.
Specifications.Base
;
public
abstract
class
Specification
<
TEntity
>
where
TEntity
:
Entity
public
abstract
class
Specification
<
TEntity
>
where
TEntity
:
Entity
{
{
...
...
Clinics.Backend/Persistence/Specifications/Evaluator/SpecificationEvaluator.cs
→
Clinics.Backend/Persistence/
Repositories/
Specifications/Evaluator/SpecificationEvaluator.cs
View file @
a23dc773
using
Domain.Primitives
;
using
Domain.Primitives
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Specifications.Base
;
using
Persistence.
Repositories.
Specifications.Base
;
namespace
Persistence.Specifications.Evaluator
;
namespace
Persistence.
Repositories.
Specifications.Evaluator
;
public
static
class
SpecificationEvaluator
public
static
class
SpecificationEvaluator
{
{
...
...
Clinics.Backend/Persistence/UnitOfWork/UnitOfWork.cs
0 → 100644
View file @
a23dc773
using
Domain.UnitOfWork
;
using
Persistence.Context
;
namespace
Persistence.UnitOfWork
;
public
class
UnitOfWork
:
IUnitOfWork
{
private
readonly
ClinicsDbContext
_context
;
public
UnitOfWork
(
ClinicsDbContext
context
)
{
_context
=
context
;
}
public
async
Task
SaveChangesAsync
()
{
try
{
await
_context
.
SaveChangesAsync
();
}
catch
(
Exception
)
{
// TODO: Log errors using ILogger
//throw;
}
}
}
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