Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Medic
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
Medic
Commits
20a9baae
Commit
20a9baae
authored
May 21, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add generic repository implementation
parent
25289efb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
GenericRepository.cs
Infrastructure/Repository/GenericRepository.cs
+67
-0
No files found.
Infrastructure/Repository/GenericRepository.cs
0 → 100644
View file @
20a9baae
using
ApplicationCore.Entities
;
using
ApplicationCore.Interfaces
;
using
Microsoft.EntityFrameworkCore
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq.Expressions
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Infrastructure.Repository
{
public
class
GenericRepository
<
T
>
:
IGenericRepository
<
T
>
where
T
:
EntityBase
{
private
readonly
DbContext
_context
;
private
DbSet
<
T
>
_table
;
public
GenericRepository
(
DbContext
context
)
{
_context
=
context
;
_table
=
_context
.
Set
<
T
>();
}
public
void
Delete
(
int
id
)
{
T
entity
=
_table
.
Find
(
id
);
_table
.
Remove
(
entity
);
}
public
IEnumerable
<
T
>
GetAll
(
params
Expression
<
Func
<
T
,
object
>>[]
includeProperties
)
{
{
IQueryable
<
T
>
queryable
=
_table
;
foreach
(
var
includeProperty
in
includeProperties
)
{
queryable
=
queryable
.
Include
(
includeProperty
);
}
return
queryable
.
AsEnumerable
();
}
}
public
T
GetById
(
int
id
,
params
Expression
<
Func
<
T
,
object
>>[]
includeProperties
)
{
IQueryable
<
T
>
query
=
_table
;
if
(
includeProperties
is
not
null
)
foreach
(
var
includeProperty
in
includeProperties
)
{
query
=
query
.
Include
(
includeProperty
);
}
return
query
.
Where
(
p
=>
p
.
Id
==
id
).
First
();
}
public
T
Insert
(
T
entity
)
{
return
_table
.
Add
(
entity
).
Entity
;
}
public
T
Update
(
T
entity
)
{
var
e
=
_table
.
Attach
(
entity
).
Entity
;
_context
.
Entry
(
entity
).
State
=
EntityState
.
Modified
;
return
e
;
}
}
}
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