Skip to content

Commit e866566

Browse files
author
Taner Saydam
committed
I added new methods
1 parent 1b64033 commit e866566

4 files changed

Lines changed: 65 additions & 12 deletions

File tree

GenericRepository/GenericRepository.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8-
<Title>TS GenericRepository</Title>
8+
<Title>TS Generic Repository</Title>
99
<Company>Taner Saydam</Company>
10-
<Description>This is a Generic Repository for Entity Framework Core with .NET Core</Description>
10+
<Description>A lightweight file service for handling uploads, saving to local or FTP storage, and deleting files using IFormFile in ASP.NET Core.</Description>
1111
<PackageIcon>logo.jpg</PackageIcon>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
13-
<RepositoryUrl>https://github.com/TanerSaydam/TS.GenericRepository.NuGet.git</RepositoryUrl>
13+
<RepositoryUrl>https://github.com/TS-NuGet-Packages/TS.EntityFrameworkCore.GenericRepository</RepositoryUrl>
1414
<PackageId>TS.EntityFrameworkCore.GenericRepository</PackageId>
1515
<AssemblyVersion></AssemblyVersion>
1616
<FileVersion></FileVersion>
17-
<Version>9.0.3</Version>
17+
<Version>9.0.4</Version>
1818
<Authors>Taner Saydam</Authors>
19-
<PackageTags>genericrepository</PackageTags>
19+
<PackageTags>genericrepository, efcore, entity framework core</PackageTags>
2020
<Product>Generic Repository</Product>
2121
<RepositoryType>git</RepositoryType>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>

GenericRepository/IRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System.Linq.Expressions;
2+
using Microsoft.EntityFrameworkCore.Query;
23

34
namespace GenericRepository;
45

56
public interface IRepository<TEntity>
67
where TEntity : class
78
{
9+
IQueryable<TEntity> AsQueryable();
810
IQueryable<TEntity> GetAll();
911
IQueryable<TEntity> GetAllWithTracking();
1012
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
@@ -36,4 +38,8 @@ public interface IRepository<TEntity>
3638
int Count(Expression<Func<TEntity, bool>> expression);
3739
Task<int> CountAsync(CancellationToken cancellationToken = default);
3840
Task<int> CountAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
41+
int ExecuteUpdate(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls);
42+
Task<int> ExecuteUpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, CancellationToken cancellationToken = default);
43+
int ExecuteDelete();
44+
Task<int> ExecuteDeleteAsync(CancellationToken cancellationToken = default);
3945
}

GenericRepository/Repository.cs

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

45
namespace GenericRepository;
56

@@ -63,6 +64,11 @@ public void DeleteRange(ICollection<TEntity> entities)
6364
Entity.RemoveRange(entities);
6465
}
6566

67+
public IQueryable<TEntity> AsQueryable()
68+
{
69+
return Entity.AsNoTracking().AsQueryable();
70+
}
71+
6672
public IQueryable<TEntity> GetAll()
6773
{
6874
return Entity.AsNoTracking().AsQueryable();
@@ -100,7 +106,6 @@ public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> e
100106
return entity;
101107
}
102108

103-
104109
public TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression)
105110
{
106111
TEntity entity = Entity.Where(expression).FirstOrDefault();
@@ -204,4 +209,24 @@ public async Task<int> CountAsync(Expression<Func<TEntity, bool>> expression, Ca
204209
{
205210
return await Entity.CountAsync(expression, cancellationToken);
206211
}
212+
213+
public int ExecuteUpdate(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls)
214+
{
215+
return Entity.ExecuteUpdate(setPropertyCalls);
216+
}
217+
218+
public async Task<int> ExecuteUpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, CancellationToken cancellationToken = default)
219+
{
220+
return await Entity.ExecuteUpdateAsync(setPropertyCalls, cancellationToken);
221+
}
222+
223+
public int ExecuteDelete()
224+
{
225+
return Entity.ExecuteDelete();
226+
}
227+
228+
public async Task<int> ExecuteDeleteAsync(CancellationToken cancellationToken = default)
229+
{
230+
return await Entity.ExecuteDeleteAsync(cancellationToken);
231+
}
207232
}

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
# GenericRepository
2+
3+
A clean, flexible, and fully-featured generic repository implementation for **Entity Framework Core**.
4+
5+
This repository provides a powerful abstraction over `DbSet<T>` and supports async LINQ queries, entity tracking control, expression-based filtering, and batch operations all designed to help you write cleaner and more testable data access code.
6+
7+
---
8+
19
# Dependency
210

311
This library was created by .Net 9.0
412

5-
## Install
13+
## Installation
614
```bash
715
dotnet add package TS.EntityFrameworkCore.GenericRepository
816
```
@@ -13,16 +21,16 @@ public class ApplicationDbContext : IUnitOfWork
1321
```
1422

1523
## Create Repository
16-
```CSharp
24+
```csharp
1725
public selead IUserRepository : IRepository<User>
1826
```
1927

20-
```CSharp
28+
```csharp
2129
public selead UserRepository : Repository<User, ApplicationDbContext>, IUserRepository
2230
```
2331

24-
## Use
25-
```Csharp
32+
## Use Examples
33+
```csharp
2634
public selead UserService: IUserService
2735

2836
private readonly IUserRepository _userRepository;
@@ -53,6 +61,15 @@ public async Task<IList<User>> GetAllAsync(CancellationToken cancellationToken)
5361
}
5462
```
5563

64+
### Execute server-side update (EF Core 7+)
65+
66+
```csharp
67+
await _repository.ExecuteUpdateAsync(set => set
68+
.SetProperty(p => p.UpdatedAt, _ => DateTime.UtcNow));
69+
```
70+
71+
---
72+
5673
## Dependency Injection
5774
```CSharp
5875
builder.Service.AddScoped<IUserRepository, UserRepository>();
@@ -67,6 +84,7 @@ IRepository, IUnitOfWork
6784
public interface IRepository<TEntity>
6885
where TEntity : class
6986
{
87+
IQueryable<TEntity> AsQueryable();
7088
IQueryable<TEntity> GetAll();
7189
IQueryable<TEntity> GetAllWithTracking();
7290
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
@@ -93,10 +111,14 @@ public interface IRepository<TEntity>
93111
Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
94112
void Delete(TEntity entity);
95113
void DeleteRange(ICollection<TEntity> entities);
96-
IQueryable<KeyValuePair<bool, int>> CountBy(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
114+
IQueryable<KeyValuePair<bool, int>> CountBy(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
97115
int Count();
98116
int Count(Expression<Func<TEntity, bool>> expression);
99117
Task<int> CountAsync(CancellationToken cancellationToken = default);
100118
Task<int> CountAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
119+
int ExecuteUpdate(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls);
120+
Task<int> ExecuteUpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, CancellationToken cancellationToken = default);
121+
int ExecuteDelete();
122+
Task<int> ExecuteDeleteAsync(CancellationToken cancellationToken = default);
101123
}
102124
```

0 commit comments

Comments
 (0)