Rapid.GenericRepositoryPattern 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rapid.GenericRepositoryPattern --version 2.0.0                
NuGet\Install-Package Rapid.GenericRepositoryPattern -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="Rapid.GenericRepositoryPattern" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rapid.GenericRepositoryPattern --version 2.0.0                
#r "nuget: Rapid.GenericRepositoryPattern, 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 Rapid.GenericRepositoryPattern as a Cake Addin
#addin nuget:?package=Rapid.GenericRepositoryPattern&version=2.0.0

// Install Rapid.GenericRepositoryPattern as a Cake Tool
#tool nuget:?package=Rapid.GenericRepositoryPattern&version=2.0.0                

Rapid.GenericRepositoryPattern

A comprehensive Generic Repository Pattern implementation for .NET applications with advanced features including async operations, specifications pattern, Entity Framework Core integration, caching, security features, and more. Supports both .NET 8 and .NET 9.

NuGet Downloads License

🌟 Features

Core Features:

  • ✨ Generic CRUD operations with async support
  • 🔄 Unit of Work pattern implementation
  • 🎯 Specification pattern for complex queries
  • 📄 Pagination and sorting capabilities
  • 🚀 Built-in caching mechanism
  • 🗑️ Soft delete support
  • 📝 Audit logging
  • 💼 Transaction management
  • 📦 Bulk operations with progress tracking
  • 💪 Strongly typed repositories
  • 🔌 Entity Framework Core integration
  • 💉 Dependency Injection support

Security Features:

  • 🔒 Advanced security features
    • Row-level security
    • Data encryption
    • SQL injection prevention
    • Access control
    • Input validation
    • Security policies

Data Management:

  • 🎭 Multi-tenancy support
  • 🔍 Health monitoring
  • 📊 Performance optimization
  • 📈 Metrics collection
  • 🔄 Event sourcing
  • 📝 Change tracking
  • 🔁 Concurrency handling
  • 🔄 Type conversions

Query Features:

  • 🔍 Dynamic LINQ support
  • 📊 Advanced projections
  • 🔄 Custom type mappings
  • 🎯 Complex filtering
  • 📋 Batch operations

Validation & Error Handling:

  • ✅ Comprehensive validation
  • 🔄 Retry policies
  • 🛡️ Error handling
  • 📝 Custom validation rules
  • 🔍 Input sanitization

Caching & Performance:

  • 💾 Multiple cache providers
  • 🚀 Cache invalidation
  • 📊 Cache statistics
  • 🔄 Cache tags
  • 📈 Performance monitoring

Monitoring & Diagnostics:

  • 📊 Health checks
  • 📈 Performance metrics
  • 🔍 Diagnostic events
  • 📝 Audit trails
  • 🔄 Operation tracking

📑 Table of Contents

📥 Installation

Package Manager Console

Install-Package Rapid.GenericRepositoryPattern

.NET CLI

dotnet add package Rapid.GenericRepositoryPattern

Framework Compatibility

  • .NET 8.0
  • .NET 9.0

🚀 Quick Start

1. Basic Setup

Register the services in your Program.cs or Startup.cs:

services.AddGenericRepository<YourDbContext>(options => 
{
    options.EnableCaching = true;
    options.DefaultCacheDurationMinutes = 30;
    options.EnableSoftDelete = true;
    options.EnableAuditLogging = true;
    options.EnableSecurity = true;
});

2. Define Your Entities

public class Customer : BaseEntity
{
    public int Id { get; set; }
    
    public string Name { get; set; }
    
    [Encrypted]
    public string SensitiveData { get; set; }
    
    public string Email { get; set; }
    
    public virtual ICollection<Order> Orders { get; set; }
}

3. Create Repository and Service

public interface ICustomerService
{
    Task<Customer> CreateAsync(Customer customer);
    Task<Customer?> GetByIdAsync(int id);
    Task<PagedResult<Customer>> GetPagedAsync(int page, int pageSize);
}

public class CustomerService : ICustomerService
{
    private readonly IRepository<Customer> _repository;
    private readonly IUnitOfWork _unitOfWork;

    public CustomerService(IRepository<Customer> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }

    public async Task<Customer> CreateAsync(Customer customer)
    {
        await _unitOfWork.BeginTransactionAsync();
        try
        {
            var result = await _repository.AddAsync(customer);
            await _unitOfWork.CommitTransactionAsync();
            return result;
        }
        catch
        {
            await _unitOfWork.RollbackTransactionAsync();
            throw;
        }
    }

    public async Task<Customer?> GetByIdAsync(int id)
    {
        return await _repository.GetByIdAsync(id);
    }

    public async Task<PagedResult<Customer>> GetPagedAsync(int page, int pageSize)
    {
        return await _repository.GetPagedAsync(page, pageSize, "Name");
    }
}

🔥 Advanced Usage

Specification Pattern

public class ActiveCustomersSpecification : BaseSpecification<Customer>
{
    public ActiveCustomersSpecification()
        : base(c => !c.IsDeleted)
    {
        AddInclude(c => c.Orders);
        ApplyOrderBy(c => c.Name);
        ApplyPaging(1, 10);
    }
}

// Usage
var spec = new ActiveCustomersSpecification();
var customers = await _repository.GetAsync(spec);

Security Features

// Row-level security
public class CustomerSecurityPolicy : ISecurityPolicy<Customer>
{
    public Expression<Func<Customer, bool>> SecurityFilter(ISecurityContext context)
    {
        return customer => customer.TenantId == context.GetCurrentTenantId();
    }
}

// Data encryption
public class Customer
{
    [Encrypted]
    public string CreditCardNumber { get; set; }
}

// SQL injection prevention
public class SafeQueryProvider : ISqlSecurityProvider
{
    public bool ValidateQuery(string sql)
    {
        // Implement validation logic
        return true;
    }
}

Caching

// Configure caching
services.AddGenericRepository<YourDbContext>(options => 
{
    options.EnableCaching = true;
    options.DefaultCacheDurationMinutes = 60;
    options.CacheKeyPrefix = "YourApp";
});

// Cached queries
var customers = await _repository.GetAllCachedAsync();
var customer = await _repository.GetByIdCachedAsync(1);

Bulk Operations

// Bulk insert
await _repository.BulkInsertAsync(customers, batchSize: 1000);

// Bulk update
await _repository.BulkUpdateAsync(
    c => c.Status == Status.Pending,
    c => new Customer { Status = Status.Processed }
);

// Bulk delete
await _repository.BulkDeleteAsync(c => c.LastModifiedAt < DateTime.UtcNow.AddYears(-1));

📈 Performance Optimization

  1. Use projections for better performance:
var dtos = await _repository.ProjectToListAsync<CustomerDto>();
  1. Enable caching for frequently accessed data:
var cachedData = await _repository.GetAllCachedAsync(TimeSpan.FromHours(1));
  1. Use bulk operations for large datasets:
await _repository.BulkInsertAsync(largeDataset);
  1. Implement pagination:
var pagedResult = await _repository.GetPagedAsync(1, 10);

📚 Documentation

🔄 Migration Guide

Upgrading to v2.x

  1. Update package reference:
<PackageReference Include="Rapid.GenericRepositoryPattern" Version="2.0.0" />
  1. Update configuration:
services.AddGenericRepository<YourDbContext>(options => 
{
    // New options available in v2.x
    options.EnableSecurity = true;
    options.UseEncryption(encryptionOptions);
});

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

📄 License

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

💬 Support

🙏 Acknowledgments

  • Entity Framework Core team
  • .NET community
  • Our contributors
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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.2 110 1/1/2025
2.0.0 92 12/31/2024
1.0.0 2,972 12/16/2024