conocimientogenerador-crudtemplates/3570 B
Service.cs.template
Fichero que /hv:generador-crud usa o materializa en el proyecto. Se muestra tal cual está en el plugin.
// ============================================================================
// TEMPLATE: {{Entity}}Service.cs - la logica de negocio (por entidad)
// Skill: generador-crud, estilo n-layer (ADR-050)
// ============================================================================
// USO: Reemplazar {{Entity}}, {{Entities}}, {{Namespace}} y los fragmentos
// {{AsignacionesDesdeRequest}}, {{AsignacionesActualizarPoco}}, {{ProyeccionDto}}
// Destino: src/Core/Services/{{Entity}}Service.cs
//
// AQUI VIVEN LAS REGLAS. La entidad de este estilo es un POCO, asi que lo que en `clean` protegeria
// la factory del dominio se comprueba en este servicio. Es un reparto distinto, no uno peor: en un
// gestor de tablas las reglas suelen ser de aplicacion ("el nombre no se repite"), no invariantes
// del modelo.
//
// El servicio depende del repositorio DIRECTAMENTE y eso es correcto en n capas: no hay inversion.
// Lo que no puede hacer es conocer la Api, y la regla de arquitectura lo comprueba.
// ============================================================================
using {{Namespace}}.Core.Data;
using {{Namespace}}.Core.Dtos;
using {{Namespace}}.Core.Entities;
namespace {{Namespace}}.Core.Services;
public interface I{{Entity}}Service
{
Task<{{Entity}}Dto?> GetAsync(int id, CancellationToken ct = default);
Task<PagedResponse<{{Entity}}Dto>> GetPageAsync(int page, int size, CancellationToken ct = default);
Task<{{Entity}}Dto> CreateAsync(Create{{Entity}}Request request, CancellationToken ct = default);
Task<bool> UpdateAsync(int id, Update{{Entity}}Request request, CancellationToken ct = default);
Task<bool> DeleteAsync(int id, CancellationToken ct = default);
}
public class {{Entity}}Service : I{{Entity}}Service
{
private readonly I{{Entity}}Repository _repo;
public {{Entity}}Service(I{{Entity}}Repository repo) => _repo = repo;
public async Task<{{Entity}}Dto?> GetAsync(int id, CancellationToken ct = default)
{
var entity = await _repo.GetByIdAsync(id, ct);
return entity is null ? null : Proyectar(entity);
}
public async Task<PagedResponse<{{Entity}}Dto>> GetPageAsync(
int page, int size, CancellationToken ct = default)
{
var (items, total) = await _repo.GetPageAsync(page, size, ct);
return new PagedResponse<{{Entity}}Dto>(items.Select(Proyectar).ToList(), total, Math.Max(1, page), size);
}
public async Task<{{Entity}}Dto> CreateAsync(Create{{Entity}}Request request, CancellationToken ct = default)
{
// La regla de negocio vive aqui, no en la entidad: ver la cabecera.
if (await _repo.ExistsNameAsync(request.Name, ct))
throw new InvalidOperationException("Name is already in use.");
var entity = new {{Entity}}();
{{AsignacionesDesdeRequest}}
await _repo.AddAsync(entity, ct);
return Proyectar(entity);
}
public async Task<bool> UpdateAsync(int id, Update{{Entity}}Request request, CancellationToken ct = default)
{
// Viene RASTREADA del repositorio: modificarla y guardar basta, sin llamar a Update.
var entity = await _repo.GetByIdAsync(id, ct);
if (entity is null) return false;
{{AsignacionesActualizarPoco}}
return await _repo.UpdateAsync(id, entity, ct);
}
public Task<bool> DeleteAsync(int id, CancellationToken ct = default) => _repo.DeleteAsync(id, ct);
private static {{Entity}}Dto Proyectar({{Entity}} e) => new()
{
Id = e.Id,
{{ProyeccionDto}}
};
}