Skip to content

Add customer projection service and event processor#13

Open
ambvdijk wants to merge 1 commit intodevelopfrom
feature/projection-v1
Open

Add customer projection service and event processor#13
ambvdijk wants to merge 1 commit intodevelopfrom
feature/projection-v1

Conversation

@ambvdijk
Copy link
Copy Markdown
Collaborator

Introduces MathMax.Administration.Projections.Customer project for handling customer event projections. Adds background services for consuming NATS events and updating PostgreSQL, configuration settings, and registers the new project in the solution file.

Introduces MathMax.Administration.Projections.Customer project for handling customer event projections. Adds background services for consuming NATS events and updating PostgreSQL, configuration settings, and registers the new project in the solution file.
@ambvdijk ambvdijk requested a review from Copilot July 10, 2025 17:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a new Customer projections service project to consume events from NATS and update a PostgreSQL database.

  • Introduces MathMax.Administration.Projections.Customer project and registers it in the solution
  • Implements CustomerProjectionService to pull events from JetStream and post to a bounded channel
  • Implements CustomerEventProcessor to read from the channel and upsert records in PostgreSQL

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
MathMax.EventSourcing.sln Registers new project and adds platform configurations
ProjectionSettings.cs Defines configuration POCO for NATS and Postgres
Program.cs Sets up DI, hard-coded settings, and background hosts
MathMax.Administration.Projections.Customer.csproj References required packages and projects
CustomerProjectionService.cs Background service that pulls and filters NATS events
CustomerEventProcessor.cs Background service that upserts customer records
Comments suppressed due to low confidence (3)

MathMax.Administration.Projections.Customer/CustomerProjectionService.cs:13

  • [nitpick] Consider adding unit and integration tests for CustomerProjectionService to verify deserialization, branching logic, and backpressure handling.
public class CustomerProjectionService : BackgroundService

MathMax.Administration.Projections.Customer/ProjectionSettings.cs:3

  • [nitpick] Public settings class lacks XML documentation. Adding <summary> comments on the class and its properties will improve discoverability when binding and using ProjectionSettings.
public class ProjectionSettings

MathMax.Administration.Projections.Customer/Program.cs:25

  • The call to Channel.CreateBounded<EventEnvelope> requires using System.Threading.Channels; and you also need using MathMax.EventSourcing; for EventEnvelope. Add these usings at the top of Program.cs so the code compiles.
                services.AddSingleton(Channel.CreateBounded<EventEnvelope>(100));

Comment on lines +15 to +21
services.AddSingleton<ProjectionSettings>(new ProjectionSettings
{
NatsUrl = "nats://localhost:4222",
StreamName = "EVENTS",
ConsumerName = "projection-consumer",
Subject = "events.>",
PostgresConnectionString = "Host=localhost;Username=postgres;Password=yourpassword;Database=yourdb"
Copy link

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coding the database connection string (including credentials) is insecure. Consider moving this into configuration files or environment variables and binding it via IConfiguration.

Suggested change
services.AddSingleton<ProjectionSettings>(new ProjectionSettings
{
NatsUrl = "nats://localhost:4222",
StreamName = "EVENTS",
ConsumerName = "projection-consumer",
Subject = "events.>",
PostgresConnectionString = "Host=localhost;Username=postgres;Password=yourpassword;Database=yourdb"
services.AddSingleton<ProjectionSettings>(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
return new ProjectionSettings
{
NatsUrl = "nats://localhost:4222",
StreamName = "EVENTS",
ConsumerName = "projection-consumer",
Subject = "events.>",
PostgresConnectionString = configuration.GetConnectionString("PostgresConnection")
};

Copilot uses AI. Check for mistakes.
var envelope = JsonSerializer.Deserialize<EventEnvelope>(Encoding.UTF8.GetString(msg.Data));
if (envelope == null)
{
Console.WriteLine("Failed to deserialize EventEnvelope");
Copy link

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using Console.WriteLine prevents structured logging and makes it hard to control output. Inject an ILogger<CustomerProjectionService> and use _logger.LogError(...) instead.

Copilot uses AI. Check for mistakes.
}
catch (Exception ex)
{
Console.WriteLine($"Error processing event in DB: {ex}");
Copy link

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Replace Console.WriteLine with a structured ILogger<CustomerEventProcessor> for better diagnostics and control over log levels.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants