conocimientogenerador-crudtemplates/7049 B
Entity.cs.template
Fichero que /hv:generador-crud usa o materializa en el proyecto. Se muestra tal cual está en el plugin.
// ============================================================================
// TEMPLATE: Entity.cs - Entidad de dominio (por entidad)
// Skill: generador-crud (ADR-031)
// ============================================================================
// USO: Reemplazar {{Entity}}, {{Namespace}} y los fragmentos {{Propiedades}}, {{ParametrosConstructor}},
// {{AsignacionesConstructor}}, {{ParametrosFactory}}, {{Validaciones}}, {{ArgumentosConstructor}},
// {{ParametrosActualizar}}, {{ValidacionesActualizar}}, {{AsignacionesActualizar}}
// Destino: src/Domain/Entities/{{Entity}}.cs
// ============================================================================
using {{Namespace}}.Domain.Exceptions;
namespace {{Namespace}}.Domain.Entities;
/// <summary>
/// Entidad de dominio: {{Entity}}. Hereda auditoria (CreatedAt/By, ModifiedAt/By) y borrado logico
/// (IsDeleted, DeletedAt/By, SoftDelete/Restore) de SoftDeleteEntity: aqui solo va lo propio de la entidad.
/// </summary>
public class {{Entity}} : SoftDeleteEntity
{
// ═══════════════════════════════════════════════════════════════════════
// PROPIEDADES - Solo getters públicos, setters privados
// ═══════════════════════════════════════════════════════════════════════
{{Propiedades}}
// ═══════════════════════════════════════════════════════════════════════
// NAVEGACIÓN (si aplica)
// ═══════════════════════════════════════════════════════════════════════
// private readonly List<RelatedEntity> _related = [];
// public IReadOnlyCollection<RelatedEntity> Related => _related.AsReadOnly();
// ═══════════════════════════════════════════════════════════════════════
// CONSTRUCTOR - Privado para forzar el uso de la factoria
// ═══════════════════════════════════════════════════════════════════════
private {{Entity}}() { } // Para el materializador de la persistencia: EF Core y Dapper lo usan igual
private {{Entity}}({{ParametrosConstructor}})
{
{{AsignacionesConstructor}}
}
// ═══════════════════════════════════════════════════════════════════════
// FACTORÍA - Punto único de creación con validación de dominio
// ═══════════════════════════════════════════════════════════════════════
public static {{Entity}} Create({{ParametrosFactory}})
{
{{Validaciones}}
var entity = new {{Entity}}({{ArgumentosConstructor}});
// Evento de dominio (opcional)
// entity.AddDomainEvent(new {{Entity}}CreatedEvent(entity.Id));
return entity;
}
// ═══════════════════════════════════════════════════════════════════════
// COMPORTAMIENTOS - Métodos con lógica de negocio
// ═══════════════════════════════════════════════════════════════════════
public void Update({{ParametrosActualizar}})
{
{{ValidacionesActualizar}}
{{AsignacionesActualizar}}
// Evento de dominio (opcional)
// AddDomainEvent(new {{Entity}}UpdatedEvent(Id));
}
// El borrado logico es SoftDelete(deletedBy) / Restore() de la base. Si la entidad necesita reglas
// propias (p. ej. no borrar una publicada), se sobreescriben:
//
// public override void SoftDelete(string? deletedBy = null)
// {
// if (Status == {{Entity}}Status.Published)
// throw new DomainException("A published {{Entity}} cannot be deleted");
// base.SoftDelete(deletedBy);
// }
}
// ============================================================================
// EJEMPLO GENERADO - Entidad "Scholarship"
// ============================================================================
/*
public class Scholarship : SoftDeleteEntity
{
public string Code { get; private set; } = null!;
public string Name { get; private set; } = null!;
public string? Description { get; private set; }
public decimal Amount { get; private set; }
public ScholarshipStatus Status { get; private set; }
public DateOnly StartDate { get; private set; }
public DateOnly EndDate { get; private set; }
private readonly List<ScholarshipApplication> _applications = [];
public IReadOnlyCollection<ScholarshipApplication> Applications => _applications.AsReadOnly();
private Scholarship() { }
private Scholarship(string code, string name, decimal amount, DateOnly startDate, DateOnly endDate)
{
Code = code;
Name = name;
Amount = amount;
StartDate = startDate;
EndDate = endDate;
Status = ScholarshipStatus.Draft;
}
public static Scholarship Create(string code, string name, decimal amount, DateOnly startDate, DateOnly endDate)
{
if (string.IsNullOrWhiteSpace(code))
throw new DomainException("Code is required");
if (string.IsNullOrWhiteSpace(name))
throw new DomainException("Name is required");
if (name.Length > 200)
throw new DomainException("Name cannot exceed 200 characters");
if (amount <= 0)
throw new DomainException("Amount must be positive");
if (endDate <= startDate)
throw new DomainException("End date must be after start date");
return new Scholarship(code.ToUpperInvariant(), name.Trim(), amount, startDate, endDate);
}
public void Publish()
{
if (Status != ScholarshipStatus.Draft)
throw new DomainException("Only draft scholarships can be published");
Status = ScholarshipStatus.Published;
}
}
public enum ScholarshipStatus { Draft = 0, Published = 1, Closed = 2, Cancelled = 3 }
*/