An open API service indexing awesome lists of open source software.

https://github.com/angelodotnet/net8-efcore-genericrepository

Collection of a generic implementation of Entity Framework Core for .NET 8 mostly used in my private and/or work projects thus avoiding the duplication of repetitive code.
https://github.com/angelodotnet/net8-efcore-genericrepository

aspnetcore c-sharp entity-framework-core generic-repository hacktoberfest hacktoberfest-accepted nuget-package

Last synced: 5 months ago
JSON representation

Collection of a generic implementation of Entity Framework Core for .NET 8 mostly used in my private and/or work projects thus avoiding the duplication of repetitive code.

Awesome Lists containing this project

README

          

# NET8 EFCore Generic Repository
Collection of a generic implementation of Entity Framework Core for .NET 8 mostly used in my private and/or work projects thus avoiding the duplication of repetitive code.

## Give a star
If you found this Implementation helpful or used it in your Projects, do give it a :star: on Github. Thanks!

## Installation
The library is available on [NuGet](https://www.nuget.org/packages/ClassLibrary.EFCore) or run the following command in the .NET CLI:

```bash
dotnet add package ClassLibrary.EFCore
```

## How to use

### Registering services

```csharp
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped();
services.AddScoped, Repository>();
```

Alternatively the generic version of the repository can be registered as follows:

```csharp
services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
```

## Important

In this README the INT type is used as ID type, but it is also possible to use the GUID type, making the appropriate corrections later.

## Example entity

```csharp
public class YourEntity : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
```

## Example interface

```csharp
public interface IYourEntityService
{
Task> GetAllAsync(Func,
IIncludableQueryable> includes = null!,
Expression> filter = null!,
Expression> orderBy = null!,
bool ascending = true);

Task GetByIdAsync(int id);
Task CreateAsync(YourEntity entity);
Task UpdateAsync(YourEntity entity);
Task DeleteAsync(YourEntity entity);

//Alternative method for deleting
Task DeleteByIdAsync(int id);

//Optional method
Task> GetPaginatedAsync(IQueryable query, int pageNumber, int pageSize);

//Optional method for pagination (Prefer this method instead of GetPaginatedAsync)
Task> GetAllPagingAsync(int pageNumber, int pageSize, Func,
IIncludableQueryable> includes = null!, Expression> filter = null!,
Expression> orderBy = null!, bool ascending = true);
}
```

## Example class

```csharp
public class YourEntityService : IYourEntityService
{
private readonly IRepository _repository;

public YourEntityService(IRepository repository)
{
_repository = repository;
}

//This method accepts optional lambdas as input (includes, where, order by),
//while by default it is sorted in ascending order (set false if you want to sort in descending order)
public async Task> GetAllAsync()
{
return await _repository.GetAllAsync();
}

public async Task GetByIdAsync(int id)
{
return await _repository.GetByIdAsync(id);
}

public async Task CreateAsync(YourEntity entity)
{
await _repository.CreateAsync(entity);
}

public async Task UpdateAsync(YourEntity entity)
{
await _repository.UpdateAsync(entity);
}

public async Task DeleteAsync(YourEntity entity)
{
await _repository.DeleteAsync(entity);
}

//Alternative method for deleting
public async Task DeleteByIdAsync(int id)
{
await _repository.DeleteByIdAsync(id);
}

//Optional method for pagination
//For optional lambdas read the comments of the GetAllAsync method
//A simple way to retrieve data is to call the GetAllAsync() method
//After retrieving the data, it will be possible to invoke the GetPaginatedAsync() method to have a paginated list
//Example: var query = await repository.GetAllAsync(); var result = await repository.GetPaginatedAsync(query, 1, 10);
public async Task> GetPaginatedAsync(IQueryable query, int pageNumber, int pageSize)
{
return await repository.GetPaginatedAsync(query, pageNumber, pageSize);
}

//Optional method for pagination (Prefer this method instead of GetPaginatedAsync)
public async Task> GetAllPagingAsync(int pageNumber, int pageSize, Func,
IIncludableQueryable> includes = null!, Expression> filter = null!,
Expression> orderBy = null!, bool ascending = true)
{
return await repository.await repository.GetAllPagingAsync(pageNumber: 2, pageSize: 5, includes: q => q.Include(p => p.Indirizzo), filter: w => w.Id <= 10);
}
}
```

## Test results

![Test Results](https://github.com/user-attachments/assets/ae02993c-6a9a-453b-961d-96c7f661d23e)

## Contributing

Contributions and/or suggestions are always welcome.