Skip to content

Commit 0c75f02

Browse files
committed
FirstOrDefaultAsync method added
1 parent ebcf257 commit 0c75f02

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

GenericRepository/GenericRepository.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageId>TS.EntityFrameworkCore.GenericRepository</PackageId>
1515
<AssemblyVersion></AssemblyVersion>
1616
<FileVersion></FileVersion>
17-
<Version>8.0.1</Version>
17+
<Version>8.0.2</Version>
1818
<Authors>Taner Saydam</Authors>
1919
<PackageTags>genericrepository</PackageTags>
2020
<Product>Generic Repository</Product>

GenericRepository/IRepository.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public interface IRepository<TEntity>
77
{
88
IQueryable<TEntity> GetAll();
99
IQueryable<TEntity> GetAllWithTracking();
10-
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
11-
IQueryable<TEntity> WhereWithTracking(Expression<Func<TEntity, bool>> expression);
10+
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
11+
IQueryable<TEntity> WhereWithTracking(Expression<Func<TEntity, bool>> expression);
12+
13+
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true);
1214
Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
1315
Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
1416
Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
@@ -23,7 +25,7 @@ public interface IRepository<TEntity>
2325
void Update(TEntity entity);
2426
void UpdateRange(ICollection<TEntity> entities);
2527
Task DeleteByIdAsync(string id);
26-
Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
28+
Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
2729
void Delete(TEntity entity);
2830
void DeleteRange(ICollection<TEntity> entities);
2931

GenericRepository/Repository.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public async Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>>
8585
return entity;
8686
}
8787

88+
public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true)
89+
{
90+
TEntity entity;
91+
if (isTrackingActive)
92+
{
93+
entity = await Entity.Where(expression).FirstOrDefaultAsync(cancellationToken);
94+
}
95+
else
96+
{
97+
entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken);
98+
}
99+
100+
return entity;
101+
}
102+
103+
88104
public TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression)
89105
{
90106
TEntity entity = Entity.Where(expression).FirstOrDefault();
@@ -128,4 +144,6 @@ public void UpdateRange(ICollection<TEntity> entities)
128144
{
129145
Entity.UpdateRange(entities);
130146
}
131-
}
147+
148+
149+
}

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This library was created by .Net 8.0
44

55
## Install
66
```bash
7-
dotnet add package EntityFrameworkCore.GenericRepository.Nuget
7+
dotnet add package TS.EntityFrameworkCore.GenericRepository
88
```
99

1010
## UnitOfWork Implementation
@@ -40,6 +40,12 @@ public async Task AddAsync(User user, CancellationToken cancellationToken)
4040
await _unitOfWork.SaveChangesAsync(cancellationToken);
4141
}
4242

43+
public async Task<User?> GetByIdAsync(Guid id, CancellationToken cancellationToken)
44+
{
45+
User? user = await _userRepository.FirstOrDefaultAsync(p=> p.Id == id, cancellationToken);
46+
return user;
47+
}
48+
4349
public async Task<IList<User>> GetAllAsync(CancellationToken cancellationToken)
4450
{
4551
IList<User> users = await _userRepository.GetAll().ToListAsync(cancellationToken);
@@ -64,7 +70,8 @@ public interface IRepository<TEntity>
6470
IQueryable<TEntity> GetAll();
6571
IQueryable<TEntity> GetAllWithTracking();
6672
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
67-
IQueryable<TEntity> WhereWithTracking(Expression<Func<TEntity, bool>> expression);
73+
IQueryable<TEntity> WhereWithTracking(Expression<Func<TEntity, bool>> expression);
74+
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true);
6875
Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
6976
Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
7077
Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
@@ -157,6 +164,21 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
157164
return Entity.AsQueryable();
158165
}
159166

167+
public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true)
168+
{
169+
TEntity entity;
170+
if (isTrackingActive)
171+
{
172+
entity = await Entity.Where(expression).FirstOrDefaultAsync(cancellationToken);
173+
}
174+
else
175+
{
176+
entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken);
177+
}
178+
179+
return entity;
180+
}
181+
160182
public TEntity GetByExpression(Expression<Func<TEntity, bool>> expression)
161183
{
162184
TEntity entity = Entity.Where(expression).AsNoTracking().FirstOrDefault();

0 commit comments

Comments
 (0)