Grav-NetCore
1.0.2
dotnet add package Grav-NetCore --version 1.0.2
NuGet\Install-Package Grav-NetCore -Version 1.0.2
<PackageReference Include="Grav-NetCore" Version="1.0.2" />
<PackageVersion Include="Grav-NetCore" Version="1.0.2" />
<PackageReference Include="Grav-NetCore" />
paket add Grav-NetCore --version 1.0.2
#r "nuget: Grav-NetCore, 1.0.2"
#:package Grav-NetCore@1.0.2
#addin nuget:?package=Grav-NetCore&version=1.0.2
#tool nuget:?package=Grav-NetCore&version=1.0.2
Grav-NetCore
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 registrosIActionResult ListarSelect(string idPropertyName, string textPropertyName): Lista para dropdownsTDTO? Recuperar(int id, string entityIdProperty, string dtoIdProperty): Obtiene un registro por IDint Borrar(int id, string entityIdProperty): Elimina un registroint Guardar(TDTO dto, string dtoIdProperty, string entityIdProperty): Inserta o actualizaTask<ActionResult<PaginatedResponse<TDTO>>> ListarPaginadoConFiltros(...): Paginación con filtrosTask<ActionResult<PaginatedResponse<TDTO>>> ListarPaginadoConQuery(...): Paginación con queries custom
MappingExtensions
TDestination MapTo<TDestination>(object source): Mapea a nuevo objetovoid 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:
ProductIdse mapea automáticamente aproductId - 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 | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.EntityFrameworkCore (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.