CD.Generic.Repository 2.0.0

dotnet add package CD.Generic.Repository --version 2.0.0                
NuGet\Install-Package CD.Generic.Repository -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CD.Generic.Repository" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CD.Generic.Repository --version 2.0.0                
#r "nuget: CD.Generic.Repository, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CD.Generic.Repository as a Cake Addin
#addin nuget:?package=CD.Generic.Repository&version=2.0.0

// Install CD.Generic.Repository as a Cake Tool
#tool nuget:?package=CD.Generic.Repository&version=2.0.0                

Generic Repository

Description

A lightweight and flexible generic repository pattern implementation for .NET applications with Entity Framework Core. Simplifies CRUD operations and query management.

Installation

dotnet add package CD.GenericRepository

Features

  • Generic Repository Pattern implementation
  • Async/Sync operation support
  • Tracking/Non-tracking queries
  • Built-in Unit of Work pattern
  • Flexible entity queries
  • Bulk operation support
  • Cancellation token support

Quick Start

  1. First, implement your DbContext:
public class AppDbContext : DbContext, IUnitOfWork
{
    public AppDbContext(DbContextOptions options) : base(options)
    {
    }
}
  1. Create your repository:
public interface IUserRepository : IRepository<User>
{
}

public class UserRepository : Repository<User, AppDbContext>, IUserRepository
{
    public UserRepository(AppDbContext context) : base(context)
    {
    }
}
  1. Use in your service what do you need:
public class UserService
{
    private readonly IUserRepository _repository;
    private readonly IUnitOfWork _unitOfWork;

    public UserService(IUserRepository repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }

    public async Task<User?> GetUserAsync(int id)
    {
        return await _repository.GetByExpressionAsync(u => u.Id == id);
    }

    public async Task CreateUserAsync(User user)
    {
        await _repository.AddAsync(user);
        await _unitOfWork.SaveChangesAsync();
    }
}
  1. Register services:
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddScoped<IUnitOfWork>(sp => sp.GetRequiredService<AppDbContext>());
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();

Available Operations

  • IQueryable<TEntity> GetAll();
  • IQueryable<TEntity> GetAllWithTracking();
  • IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
  • IQueryable<TEntity> WhereWithTracking(Expression<Func<TEntity, bool>> expression);
  • TEntity First(Expression<Func<TEntity, bool>> expression, bool isTrackingActive = true);
  • TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression, bool isTrackingActive = true);
  • Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true);
  • Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default, bool isTrackingActive = true);
  • Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
  • Task<TEntity> GetByExpressionWithTrackingAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
  • Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
  • Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
  • bool Any(Expression<Func<TEntity, bool>> expression);
  • TEntity GetByExpression(Expression<Func<TEntity, bool>> expression);
  • TEntity GetByExpressionWithTracking(Expression<Func<TEntity, bool>> expression);
  • TEntity GetFirst();
  • Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
  • void Add(TEntity entity);
  • Task AddRangeAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default);
  • void AddRange(ICollection<TEntity> entities);
  • void Update(TEntity entity);
  • void UpdateRange(ICollection<TEntity> entities);
  • Task DeleteByIdAsync(string id);
  • Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
  • void Delete(TEntity entity);
  • void DeleteRange(ICollection<TEntity> entities);
  • IQueryable<KeyValuePair<bool, int>> CountBy(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);

Requirements

  • .NET 9.0 or later
  • Entity Framework Core 9.0 or later

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 60 2/15/2025
1.0.0 51 2/13/2025