Modules.Payments/
??? Domain/ ? CORE (sin dependencias)
? ??? Entities/
? ? ??? Payment.cs
? ??? (Events y Exceptions pendientes)
?
??? Application/ ? PUERTOS (interfaces)
? ??? Contracts/
? ? ??? Persistence/
? ? ? ??? IPaymentRepository.cs ? Interface definida aquí
? ? ??? Infrastructure/
? ? ??? IKafkaMessagePublisher.cs ? Interface definida aquí
? ??? CreatePayment/
? ? ??? CreatePaymentCommand.cs
? ? ??? CreatePaymentCommandValidator.cs
? ? ??? CreatePaymentCommandHandler.cs
? ??? GetPaymentById/
? ? ??? GetPaymentByIdQuery.cs
? ? ??? GetPaymentByIdQueryHandler.cs
? ??? UpdatePaymentStatus/
? ??? UpdatePaymentStatusCommand.cs
? ??? UpdatePaymentStatusCommandHandler.cs
?
??? Infrastructure/ ? CONFIGURACIÓN EF
? ??? Persistence/
? ??? PaymentEntityConfiguration.cs
?
??? Presentation/ ? API CONTROLLERS
? ??? DTOs/
? ? ??? CreatePaymentRequest.cs
? ? ??? PaymentResponse.cs
? ? ??? PaymentStatusResponse.cs
? ??? PaymentsController.cs
?
??? PaymentsModule.cs ? MODULE INITIALIZER
Infrastructure/
??? Repositories/
? ??? PaymentRepository.cs ? Implementa IPaymentRepository
??? Adapters/
? ??? KafkaMessagePublisherAdapter.cs ? Implementa IKafkaMessagePublisher
??? Consumers/
??? RiskEvaluationResponseConsumer.cs ? Consumer de Kafka
???????????????????????????????????????????????????
? Presentation Layer (Controllers) ?
? - Depende de: Application ?
???????????????????????????????????????????????????
?
???????????????????????????????????????????????????
? Application Layer (Handlers, Commands) ?
? - Define interfaces (IPaymentRepository) ?
? - NO depende de Infrastructure ?
? - Depende de: Domain, Shared ?
???????????????????????????????????????????????????
?
???????????????????????????????????????????????????
? Domain Layer (Entities) ?
? - NO tiene dependencias ?
? - Core puro del negocio ?
???????????????????????????????????????????????????
???????????????????????????????????????????????????
? Infrastructure Layer (Repositories, Adapters) ?
? - Implementa interfaces de Application ?
? - Depende de: Application, Domain ?
? - Tiene acceso a EF, Kafka, etc. ?
???????????????????????????????????????????????????
? Dependency Inversion Principle aplicado correctamente
Domain/Entities/Payment.cs- Entidad principal
Application/Contracts/Persistence/IPaymentRepository.cs- PuertoApplication/Contracts/Infrastructure/IKafkaMessagePublisher.cs- PuertoApplication/CreatePayment/- 3 archivos (Command, Validator, Handler)Application/GetPaymentById/- 2 archivos (Query, Handler)Application/UpdatePaymentStatus/- 2 archivos (Command, Handler)
Presentation/DTOs/- 3 archivos (Request, Response, StatusResponse)Presentation/PaymentsController.cs- API Controller
Infrastructure/Persistence/PaymentEntityConfiguration.cs- EF Configuration
PaymentsModule.cs- IModuleInitializer
// Infrastructure/Repositories/PaymentRepository.cs
public class PaymentRepository : IPaymentRepository // ? Implementa interface del módulo
{
private readonly ApplicationDbContext _context;
// Implementación con EF Core
}// Infrastructure/Adapters/KafkaMessagePublisherAdapter.cs
public class KafkaMessagePublisherAdapter : IKafkaMessagePublisher // ? Implementa interface del módulo
{
private readonly IKafkaProducer _kafkaProducer;
// Adapta la llamada al KafkaProducer real
}// Infrastructure/Consumers/RiskEvaluationResponseConsumer.cs
public class RiskEvaluationResponseConsumer : KafkaConsumerBase<RiskEvaluationResponseMessage>
{
// Escucha respuestas de evaluación de riesgo
// Actualiza Payment.Status en la base de datos
}El registro se hará en el Program.cs del proyecto API:
// Registrar Infrastructure (Adaptadores)
services.AddScoped<IPaymentRepository, PaymentRepository>();
services.AddScoped<IKafkaMessagePublisher, KafkaMessagePublisherAdapter>();
// Registrar Consumer
services.AddSingleton<RiskEvaluationResponseConsumer>();
// Registrar Módulos
services.AddScoped<IModuleInitializer, PaymentsModule>();
// Ejecutar ConfigureServices de cada módulo<PackageReference Include="FluentValidation" Version="12.1.1" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.11" />public class CreatePaymentCommandValidator : AbstractValidator<CreatePaymentCommand>
{
public CreatePaymentCommandValidator()
{
RuleFor(x => x.CustomerId).NotEmpty();
RuleFor(x => x.ServiceProviderId).NotEmpty();
RuleFor(x => x.PaymentMethodId).GreaterThan(0);
RuleFor(x => x.Amount)
.GreaterThan(0)
.LessThanOrEqualTo(10000);
}
}- Request:
CreatePaymentRequest - Validaciones: FluentValidation
- Proceso:
- Valida request
- Genera
externalOperationId - Crea
Paymentcon status "Evaluating" - Publica mensaje a Kafka
- Response:
201 CreatedconPaymentResponse
- Request: GUID en ruta
- Proceso:
- Busca
PaymentporexternalOperationId - Retorna información completa
- Busca
- Response:
200 OKconPaymentStatusResponse
var result = await _handler.HandleAsync(command, cancellationToken);
if (result.IsFailure)
{
return StatusCode(500, new ProblemDetails { Detail = result.Error });
}if (!validationResult.IsValid)
{
return BadRequest(new ProblemDetails
{
Detail = string.Join("; ", validationResult.Errors)
});
}public class RiskEvaluationResponseConsumer : KafkaConsumerBase<RiskEvaluationResponseMessage>
{
protected override async Task ProcessMessageAsync(
RiskEvaluationResponseMessage message,
CancellationToken cancellationToken)
{
// 1. Buscar Payment por ExternalOperationId
// 2. Actualizar Status (Accepted/Denied)
// 3. Actualizar UpdatedAt
// 4. SaveChanges
}
}-
S - Single Responsibility
- Cada handler tiene una única responsabilidad
- Controllers solo coordinan
-
O - Open/Closed
- Extensible mediante nuevos Commands/Queries
- Cerrado para modificación
-
L - Liskov Substitution
- Interfaces claramente definidas
- Implementaciones intercambiables
-
I - Interface Segregation
- Interfaces pequeñas y específicas
- IPaymentRepository, IKafkaMessagePublisher
-
D - Dependency Inversion
- Application define interfaces (puertos)
- Infrastructure implementa (adaptadores)
- NO hay dependencia de Application ? Infrastructure
- ? Clean Architecture (Capas bien definidas)
- ? Ports & Adapters (Hexagonal)
- ? CQRS (Commands y Queries separados)
- ? Repository Pattern
- ? Unit of Work Pattern
- ? Result Pattern (Manejo funcional de errores)
- ? Validator Pattern (FluentValidation)
- ? Module Pattern (IModuleInitializer)
- ? Adapter Pattern (KafkaMessagePublisherAdapter)
- ? Consumer Pattern (KafkaConsumerBase)
dotnet buildResultado: ? Build succeeded in 1.9s
Sin errores ?
Sin warnings ?
- Registrar servicios en Program.cs del API
- Probar endpoints con Swagger
- Implementar módulo de RiskEvaluation (PASO 5)
Fecha de completación: 2025-12-29
Estado: ? COMPLETADO
Clean Architecture: ? 100% Respetada
Líneas de código: ~1,200 líneas
Archivos creados: 17 archivos en el módulo + 2 adaptadores