Grav-NetCore 1.0.2

dotnet add package Grav-NetCore --version 1.0.2
                    
NuGet\Install-Package Grav-NetCore -Version 1.0.2
                    
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="Grav-NetCore" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Grav-NetCore" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Grav-NetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Grav-NetCore --version 1.0.2
                    
#r "nuget: Grav-NetCore, 1.0.2"
                    
#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.
#:package Grav-NetCore@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Grav-NetCore&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Grav-NetCore&version=1.0.2
                    
Install as a Cake Tool

Grav-NetCore

NuGet License: MIT

Librería genérica para operaciones CRUD con Entity Framework Core, incluyendo paginación, filtros, ordenamiento y mapeo automático entre entidades y DTOs.

Características

  • Servicio CRUD genérico: Reduce código repetitivo en tus controladores
  • Paginación integrada: Soporte completo para listados paginados con filtros y ordenamiento
  • Mapeo automático: Conversión entre entidades y DTOs sin configuración adicional
  • Queries personalizadas: Soporte para joins y queries complejas con LINQ
  • Multi-targeting: Compatible con .NET 6.0, 7.0 y 8.0

Instalación

dotnet add package Grav-NetCore

O agrega la referencia directamente en tu archivo .csproj:

<PackageReference Include="Grav-NetCore" Version="1.0.0" />

Uso Básico

1. Define tu entidad y DTO

// Entidad de base de datos
public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// DTO
public class ProductDTO
{
    public int productId { get; set; }  // Case-insensitive mapping
    public string name { get; set; }
    public decimal price { get; set; }
}

2. Configura el servicio en Program.cs

using GravNetCore.Services;

var builder = WebApplication.CreateBuilder(args);

// Registra el DbContext
builder.Services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(connectionString));

// Registra el servicio genérico
builder.Services.AddScoped<IGenericService<Product, ProductDTO, MyDbContext>,
                            GenericService<Product, ProductDTO, MyDbContext>>();

var app = builder.Build();

3. Usa el servicio en tu controlador

using GravNetCore.Services;
using GravNetCore.Helpers;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IGenericService<Product, ProductDTO, MyDbContext> _service;

    public ProductsController(IGenericService<Product, ProductDTO, MyDbContext> service)
    {
        _service = service;
    }

    [HttpGet]
    public List<ProductDTO> GetAll()
    {
        return _service.ListarCompleto();
    }

    [HttpGet("{id}")]
    public ProductDTO? GetById(int id)
    {
        return _service.Recuperar(id, "ProductId", "productId");
    }

    [HttpPost]
    public int Create([FromBody] ProductDTO dto)
    {
        return _service.Guardar(dto, "productId", "ProductId");
    }

    [HttpPut]
    public int Update([FromBody] ProductDTO dto)
    {
        return _service.Guardar(dto, "productId", "ProductId");
    }

    [HttpDelete("{id}")]
    public int Delete(int id)
    {
        return _service.Borrar(id, "ProductId");
    }

    [HttpGet("paginated")]
    public async Task<ActionResult<PaginatedResponse<ProductDTO>>> GetPaginated(
        int pageNumber = 1,
        int pageSize = 10,
        string orderBy = "productId",
        string direction = "asc")
    {
        return await _service.ListarPaginadoConFiltros(
            pageNumber,
            pageSize,
            direction,
            orderBy,
            filtroCustom: p => p.Price > 0  // Filtro opcional
        );
    }

    [HttpGet("select")]
    public IActionResult GetForDropdown()
    {
        // Retorna objetos {value, label} para dropdowns
        return _service.ListarSelect("ProductId", "Name");
    }
}

Uso Avanzado: Queries con Joins

Para consultas que requieren joins o proyecciones complejas:

[HttpGet("with-category")]
public async Task<ActionResult<PaginatedResponse<ProductWithCategoryDTO>>> GetWithCategory(
    int pageNumber = 1,
    int pageSize = 10)
{
    return await _service.ListarPaginadoConQuery(
        queryBuilder: context => context.Products
            .Join(context.Categories,
                p => p.CategoryId,
                c => c.CategoryId,
                (p, c) => new ProductWithCategoryDTO
                {
                    productId = p.ProductId,
                    name = p.Name,
                    categoryName = c.Name
                }),
        pageNumber: pageNumber,
        pageSize: pageSize,
        ascOrDesc: "asc",
        orderBy: "name"
    );
}

API Reference

IGenericService<TEntity, TDTO, TContext>

Métodos
  • List<TDTO> ListarCompleto(): Obtiene todos los registros
  • IActionResult ListarSelect(string idPropertyName, string textPropertyName): Lista para dropdowns
  • TDTO? Recuperar(int id, string entityIdProperty, string dtoIdProperty): Obtiene un registro por ID
  • int Borrar(int id, string entityIdProperty): Elimina un registro
  • int Guardar(TDTO dto, string dtoIdProperty, string entityIdProperty): Inserta o actualiza
  • Task<ActionResult<PaginatedResponse<TDTO>>> ListarPaginadoConFiltros(...): Paginación con filtros
  • Task<ActionResult<PaginatedResponse<TDTO>>> ListarPaginadoConQuery(...): Paginación con queries custom

MappingExtensions

  • TDestination MapTo<TDestination>(object source): Mapea a nuevo objeto
  • void MapProperties(object source, object destination): Mapea a objeto existente

PaginatedResponse<T>

public class PaginatedResponse<T>
{
    public int TotalRecords { get; set; }
    public List<T> Data { get; set; }
}

Características del Mapeo

  • Case-insensitive: ProductId se mapea automáticamente a productId
  • Nullable-compatible: Maneja conversiones entre tipos nullable y no-nullable
  • Type-safe: Valida compatibilidad de tipos antes de mapear
  • Bidireccional: Funciona de Entity → DTO y DTO → Entity

Contribuciones

Las contribuciones son bienvenidas. Por favor, abre un issue o pull request en el repositorio.

Licencia

Este proyecto está licenciado bajo la Licencia MIT. Ver el archivo LICENSE para más detalles.

Soporte

Para reportar bugs o solicitar features, por favor abre un issue en GitHub.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.2 379 11/25/2025
1.0.1 185 11/25/2025
1.0.0 184 11/25/2025