Skip to content

Commit e5e10f3

Browse files
committed
Added tracking methods and fix unit of work methods
1 parent 5d4dbd7 commit e5e10f3

5 files changed

Lines changed: 60 additions & 5 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>EntityFrameworkCore.GenericRepository.Nuget</PackageId>
1515
<AssemblyVersion></AssemblyVersion>
1616
<FileVersion></FileVersion>
17-
<Version>8.0.0</Version>
17+
<Version>8.0.1</Version>
1818
<Authors>Taner Saydam</Authors>
1919
<PackageTags>genericrepository</PackageTags>
2020
</PropertyGroup>

GenericRepository/IRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ public interface IRepository<TEntity>
66
where TEntity : class
77
{
88
IQueryable<TEntity> GetAll();
9+
IQueryable<TEntity> GetAllWithTacking();
910
IQueryable<TEntity> GetWhere(Expression<Func<TEntity, bool>> expression);
11+
IQueryable<TEntity> GetWhereWithTracking(Expression<Func<TEntity, bool>> expression);
1012
Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
13+
Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
1114
Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
1215
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
1316
bool Any(Expression<Func<TEntity, bool>> expression);
1417
TEntity GetByExpression(Expression<Func<TEntity, bool>> expression);
18+
TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression);
1519
TEntity GetFirst();
1620
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
1721
void Add(TEntity entity);

GenericRepository/IUnitOfWork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
public interface IUnitOfWork
44
{
55
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
6-
void SaveChanges();
6+
int SaveChanges();
77
}

GenericRepository/Repository.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using System.Linq.Expressions;
3+
using System.Threading;
34

45
namespace GenericRepository;
56

@@ -68,6 +69,11 @@ public IQueryable<TEntity> GetAll()
6869
return Entity.AsNoTracking().AsQueryable();
6970
}
7071

72+
public IQueryable<TEntity> GetAllWithTacking()
73+
{
74+
return Entity.AsQueryable();
75+
}
76+
7177
public TEntity GetByExpression(Expression<Func<TEntity, bool>> expression)
7278
{
7379
TEntity entity = Entity.Where(expression).AsNoTracking().FirstOrDefault();
@@ -78,7 +84,19 @@ public async Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>>
7884
{
7985
TEntity entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
8086
return entity;
81-
}
87+
}
88+
89+
public TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression)
90+
{
91+
TEntity entity = Entity.Where(expression).FirstOrDefault();
92+
return entity;
93+
}
94+
95+
public async Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default)
96+
{
97+
TEntity entity = await Entity.Where(expression).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
98+
return entity;
99+
}
82100

83101
public TEntity GetFirst()
84102
{
@@ -97,6 +115,11 @@ public IQueryable<TEntity> GetWhere(Expression<Func<TEntity, bool>> expression)
97115
return Entity.AsNoTracking().Where(expression).AsQueryable();
98116
}
99117

118+
public IQueryable<TEntity> GetWhereWithTracking(Expression<Func<TEntity, bool>> expression)
119+
{
120+
return Entity.Where(expression).AsQueryable();
121+
}
122+
100123
public void Update(TEntity entity)
101124
{
102125
Entity.Update(entity);

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ public interface IRepository<TEntity>
6262
where TEntity : class
6363
{
6464
IQueryable<TEntity> GetAll();
65+
IQueryable<TEntity> GetAllWithTacking();
6566
IQueryable<TEntity> GetWhere(Expression<Func<TEntity, bool>> expression);
67+
IQueryable<TEntity> GetWhereWithTracking(Expression<Func<TEntity, bool>> expression);
6668
Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
69+
Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
6770
Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
6871
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
6972
bool Any(Expression<Func<TEntity, bool>> expression);
7073
TEntity GetByExpression(Expression<Func<TEntity, bool>> expression);
74+
TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression);
7175
TEntity GetFirst();
7276
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
7377
void Add(TEntity entity);
@@ -80,7 +84,9 @@ public interface IRepository<TEntity>
8084
void DeleteRange(ICollection<TEntity> entities);
8185

8286
}
87+
```
8388

89+
```Csharp
8490
public class Repository<TEntity, TContext> : IRepository<TEntity>
8591
where TEntity : class
8692
where TContext : DbContext
@@ -146,6 +152,11 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
146152
return Entity.AsNoTracking().AsQueryable();
147153
}
148154

155+
public IQueryable<TEntity> GetAllWithTacking()
156+
{
157+
return Entity.AsQueryable();
158+
}
159+
149160
public TEntity GetByExpression(Expression<Func<TEntity, bool>> expression)
150161
{
151162
TEntity entity = Entity.Where(expression).AsNoTracking().FirstOrDefault();
@@ -156,7 +167,19 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
156167
{
157168
TEntity entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
158169
return entity;
159-
}
170+
}
171+
172+
public TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression)
173+
{
174+
TEntity entity = Entity.Where(expression).FirstOrDefault();
175+
return entity;
176+
}
177+
178+
public async Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default)
179+
{
180+
TEntity entity = await Entity.Where(expression).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
181+
return entity;
182+
}
160183

161184
public TEntity GetFirst()
162185
{
@@ -175,6 +198,11 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
175198
return Entity.AsNoTracking().Where(expression).AsQueryable();
176199
}
177200

201+
public IQueryable<TEntity> GetWhereWithTracking(Expression<Func<TEntity, bool>> expression)
202+
{
203+
return Entity.Where(expression).AsQueryable();
204+
}
205+
178206
public void Update(TEntity entity)
179207
{
180208
Entity.Update(entity);
@@ -193,6 +221,6 @@ public class Repository<TEntity, TContext> : IRepository<TEntity>
193221
public interface IUnitOfWork
194222
{
195223
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
196-
void SaveChanges();
224+
int SaveChanges();
197225
}
198226
```

0 commit comments

Comments
 (0)